nginx 反向代理 接口请求
项目在开发的时候 使用本地环境 node ,但是线上走的是 nginx 服务器 使用 vue-cli 3.0+
本地配置代理接口地址
在根目录下的 vue.config.js
function target(){
return process.env.VUE_APP_API_URL;
}
module.exports = {
devServer: {
proxy:{
'/api':{
target: target(),
changeOrigin: true,
ws: true,
}
}
}
}
nginx
目录 /usr/local/nginx/conf/vhost 下面找到你配置文件
新增你接口地址
location /api {
add_header 'Access-Control-Allow-Origin' '*';
proxy_pass https://api.*****.com;
}
这样配置 即可 访问你的api接口
建议先看我写的 vue-cli 3.0 + 封装 axios
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: