一:开发环境跨域代理
# URL代理拦截
1.在 .env.development 文件内配置:
# 环境变量以 VUE_APP_ 为开头。如:VUE_APP_API
# 开发环境配置
ENV = 'development'
# 开发环境请求后端api
VUE_APP_BASE_API = '/dev-api'
VUE_APP_OAUTH_API = '/oauth-api'
--------------------------------------------------------
2.在 vue.config.js 内配置:
devServer: {
host: '0.0.0.0',
port: port,
open: true,
proxy: {
// detail: https://cli.vuejs.org/config/#devserver-proxy
[process.env.VUE_APP_BASE_API]: {
target: `http://localhost:9527`, // localhost
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
},
[process.env.VUE_APP_OAUTH_API]: {
target: `http://localhost:9528`, // 多跨域配置
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_OAUTH_API]: ''
}
}
},
disableHostCheck: true
}
--------------------------------------------------------
3.在对应的api内引入使用,例子
const OAUTH_URL = process.env.VUE_APP_OAUTH_API
export function getCode(query) {
return request({
url: OAUTH_URL + '/oauth/authorize',
method: 'get',
params: query
})
}
二:生产环境Nginx跨域代理
在 .env.production 内配置:
# 生产环境配置
ENV = 'production'
# 生产环境
VUE_APP_BASE_API = '/md-prod-api'
VUE_APP_OAUTH_API = '/md-oauth-api'
-----------------------------------------
在 nginx.conf 内配置:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 项目根路径
location / {
root /vueui/dist;
// 说明:如果vue项目的router是 mode: 'history',那么加下面一行。
//注意打包时,publicPath: process.env.NODE_ENV === 'production' ? '/' : '/', 是否要加 ‘./’,要根据root的地址而定
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 配置代理转发(/md-prod-api 是.env.production 内定义的)
location /md-prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:9596/md/; // 后端接口
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 需要Nginx监听多个端口,就多复制一份server{}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}