現(xiàn)象
nginx轉(zhuǎn)發(fā)后端服務(wù)報(bào)錯(cuò)澎剥,應(yīng)是502锡溯,但是返回值卻是404
分析
- 查看nginx配置文件,有如下一段
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
- 報(bào)錯(cuò)404是因?yàn)榉祷?50* 之后,會(huì)轉(zhuǎn)到 /50x.html 頁(yè)面祭饭,而找不到50x.html報(bào)的 404錯(cuò)誤
解決
方法一
將上邊那一段整個(gè)注釋掉方法二
在根目錄下添加 50x.html頁(yè)面
測(cè)試
- nginx配置文件中添加或修改為如下內(nèi)容:
server {
listen 80;
server_name web80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
add_header Cache-Control no-store;
}
# 寫一個(gè)location芜茵,轉(zhuǎn)發(fā)給不存在的頁(yè)面。
location /test/ {
proxy_pass http://127.0.0.1:82/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
-
測(cè)試1
訪問 IP/test時(shí)倡蝙,本來(lái)應(yīng)該是500九串,卻報(bào)錯(cuò)404
-
測(cè)試2
在根目錄添加一個(gè) 50x.html文件,內(nèi)容為"erro 50x"寺鸥,訪問 IP/test 猪钮,顯示 50x.html的內(nèi)容
image.png
-
測(cè)試3
注釋掉錯(cuò)誤轉(zhuǎn)發(fā)一段,返回500
image.png