vue cli 解决跨域 线上 nginx 反向代理配置

九號

vue cli 解决跨域 线上 nginx 反向代理配置

1、开发环境跨域

vue cli2中,在/config/index.js里找到proxyTable开启代理changeOrigin:ture;

1
2
3
4
5
6
7
8
9
10
11
proxyTable: {
//解决
publicPath: "./",
'/api':{
target:'http://xx.com',
changeOrigin:true,
pathRewrite:{
'^/api':'/api'
}
}
},

vue cli3中的vue.config.js(如果没有可以自行创建。注:与package.json同级目录)修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = {
publicPath: "./",
devServer: {
proxy: {
"/api": {
target: "http://xx.com",
changeOrigin: true,
ws: true,
secure: false,
pathRewrite: {
"^/api": "",
},
},
},
},
};

2、nginx反向代理设置

如果使用宝塔控制面板,请在当前站点文件夹下进行配置(位置如下图所示)

在配置文件(xx.conf)的server{}中按需加入如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
location /api/ {
# 把 /api 路径下的请求转发给真正的后端服务器
proxy_pass http://xx.xx.xx.xx:3000/;
#注意端口后需加/

# 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是localhost:8080
proxy_set_header Host $http_host;

# 把cookie中的path部分从/api替换成/service
proxy_cookie_path /api /;

# 把cookie的path部分从localhost:8080替换成your.domain.name
proxy_cookie_domain localhost:80 http://xx.xx.xx.xx:3000/;
}
  • Title: vue cli 解决跨域 线上 nginx 反向代理配置
  • Author: 九號
  • Created at : 2020-05-03 10:00:00
  • Updated at : 2024-03-17 22:19:50
  • Link: https://jhao.me/posts/61269/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
vue cli 解决跨域 线上 nginx 反向代理配置