vue+nginx代理配置,解决跨域问题
举例登录接口,ip地址是随便举例的,实际生成地址域名,不一定是本地ip。
前端首页地址是:http://192.168.1.25:8080/#/login,
前端登录的地址是:http://192.168.1.25:8080/api/login
后端登录接口地址是:http://192.168.1.24:8081/login
1、vue项目的config里的Index.js
proxyTable: {
'/api': {
target: 'http://192.168.1.24:8081',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},
2、nginx配置
server {
listen 8081;
server_name 127.0.0.1;
charset utf8;
#access_log logs/host.access.log main;
location /{
root /usr/local/tomcat/webapps/ROOT;
index index.html index.htm;
}
location /api{
rewrite ^.+api/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://192.168.1.24:8081;
}
}
为什么需要这样处理呢?因为前后端分离了,有可能会出现跨域问题。
其实有没有其他办法,不需要这样代理的。
当然有的,可以部署一个Servlet项目作为sdk,然后再由sdk做转发到你的后端地址即可。
还有其他办法的,可以私信教一下我。