1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Vue Nginx反向代理配置 解决生产环境跨域

Vue Nginx反向代理配置 解决生产环境跨域

时间:2022-02-22 05:50:26

相关推荐

Vue Nginx反向代理配置 解决生产环境跨域

Vue本地代理举例:

module.exports = {publicPath: './',devServer: {proxy: {'/api': {target: '',ws: true,changeOrigin: true,pathRewrite: {'^/api': ''}},'/bpi': {target: '/',ws: true,changeOrigin: true,pathRewrite: {'^/bpi': ''}}}},pwa: {iconPaths: {favicon32: 'favicon.ico',favicon16: 'favicon.ico',appleTouchIcon: 'favicon.ico',maskIcon: 'favicon.ico',msTileImage: 'favicon.ico'}}}

Vue 本地代理编辑好后,能实现跨域获取接口数据,但是打包后在生产环境接口报错404,要怎样才能解决生产环境跨域问题呢?

在开发环境配置好本地代理后,使用Nginx反向代理解决生产环境跨域问题!

1、修改Nginx的配置文件 xxx.conf

location /api {rewrite ^.+api/?(.*)$ /$1 break; //可选参数,正则验证地址include uwsgi_params; //可选参数,uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回proxy_pass :444; #此处修改为自己的请求地址,必填}###api为本地开发时,在config/index.js中的proxyTable: {}配置的请求代理###根据具体情况修改

2、记得重启Nginx服务,使修改生效

举例:

location /api {rewrite ^.+api/?(.*)$ /$1 break;include uwsgi_params;proxy_pass ;}location /bpi {rewrite ^.+bpi/?(.*)$ /$1 break;include uwsgi_params;proxy_pass /;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。