1、nginx conf文件夾中nginx.conf文件:
server {
listen 8080;
server_name localhost;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
root E:/h5fromremote/dist;
index index.html;
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
location ~ ^/app[/\w*]*$ {
proxy_pass http://www.ee.rrrr.com;
}
location ~ ^/api[/\w*]*$ {
proxy_pass http://www.ee.rrrr.com;
}
location ~ ^/v {
proxy_pass http://www.ee.rrr.com;
}
}
(1)端口號與項目中配置的一致
(2)root 為項目打包后的本地路徑
(3)vue路由配置(加上竟然訪問路由失敗404,因為誤開了多個nginx服務)
// 這里設置默認入口是dist中的index文件
location / {
try_files $uri $uri/ @router;
index index.html;
}
// 設置Vue路由都指向index.html中
location @router {
rewrite ^.*$ /index.html last;
}
(4)動態(tài)接口路徑
location ~ ^/app[/\w*]*$ {
proxy_pass http://www.ee.rrrr.com;
}
location ~ ^/api[/\w*]*$ {
proxy_pass http://www.ee.rrrr.com;
}
location ~ ^/v {
proxy_pass http://www.ee.rrr.com;
}
在vue項目中config的index.js中 有如下配置:
proxyTable: {
'/api': {
target: 'http://www.ee.rrr.com',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
},
'/v': {
target: 'http://www.ee.rrr.com',
changeOrigin: true,
pathRewrite: {
'^/v': '/v'
}
},
'/app': {
target: 'http://www.ee.rrr.com',
changeOrigin: true,
pathRewrite: {
'^/app': '/app'
}
}
}
2、當訪問請求接口時首先訪問localhost:8080/app/list
nginx會代理到http://www.ee.rrr.com/app/list 去請求接口