location 后面地址的說明
location /api/esbapi nginx 會對前端訪問的地址進行截取,例如前端訪問地址是 http://localhost:4800/api/esbapi/manager/cm0004 那么截取后 剩余的地址為 /manager/cm0004
,然后再加上 proxy_pass= http://127.0.0.1:56668/esbapi 代理的后端地址后,實際訪問的地址為 http://127.0.0.1:56668/esbapi/manager/cm0004 。
fontEndUrl=http://localhost:4800/api/esbapi/manager/cm0004
split_address=fontEndUrl - /api/esbapi
= /manager/cm0004
backEndUrl= http://127.0.0.1:56668/esbapi
realPath= backEndUrl + split_address
= http://127.0.0.1:56668/esbapi/manager/cm0004
配置文件實例
server{
listen 4800; # 配置端口
server_name localhost; # 配置域名
charset utf-8; # 編碼
access_log C:/Users/Administrator/Desktop/nginx-1.12.2/logs/access.log main; # 成功日志
error_log C:/Users/Administrator/Desktop/nginx-1.12.2/logs/error.log error; # 錯誤日志
index index.html; # 查找文件順序
# 其余location
#靜態資源訪問
location / {
#------------------前台資源跟路徑------------------
root C:/Users/Administrator/Desktop/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://127.0.0.1:56668;
}
#/api/esbapi 對前端請求的匹配和地址截取
#http://localhost:4800/api/esbapi/manager/cm0004 截取后保留 /manager/cm0004
#然后定位到后端地址 /esbapi/manager/cm0004 其中esbapi是后端的 context-path
location /api/esbapi {
proxy_pass http://127.0.0.1:56668/esbapi;
}
location /api/mqcore {
proxy_pass http://127.0.0.1:56666/mqcore;
}
location /api/mqtask {
proxy_pass http://127.0.0.1:56669/mqtask;
}
}