从根本解决跨域(nginx部署解决方案)
配置本地host
···
127.0.0.1 rd.max.com
127.0.0.1 fe.max.com
···
配置nginx.conf
upstream jianshubao-rd {
server 192.168.0.102:8082;
}
upstream jianshubao-fe {
server 192.168.0.103:8080;
}
server {
listen 80;
server_name fe.max.com;
location / {
proxy_pass http://jianshubao-fe;
index index.html index.htm;
}
location /fe {
rewrite ^.+fe/?(.*)$ /$1 break;
#proxy_pass http://jianshubao-fe;
proxy_pass http://localhost:82;
index index.html index.htm;
}
}
server {
listen 82;
server_name rd.max.com;
location / {
proxy_pass http://jianshubao-rd;
index index.html index.htm;
}
}
反向代理的职责就是用户不知道访问的是哪台服务器
通过监听80端口,将/fe后缀的请求重定向到本地的82端口
通过upstream定义 让两台主机的server重定向到一台主机的80端口 和82端口上
进而实现前后端分离,前后端又在一个域下面
但是这样使用起来产生了一个新的问题,前端ajax请求后端的时候响应速度慢了很多。
有可能是 百兆网卡的问题,还有可能是配置的不是很完满。
不过先实现再优化是我们的开发原则。
遇到困难或技术交流,请联系我
from:郭晗
国内邮箱: 17004957578@163.com
国外邮箱: guohan02@gmail.com
微信: guohan02baidu
浙公网安备 33010602011771号