我有以下nginx.conf部分:
# valid url
location /foo/bar/ {
proxy_pass http://my_server/foo/bar/;
}
# redirect base url to index.html
location = / {
root /usr/share/nginx/html;
index index.html;
}
# anything not listed above to 404
location / {
return 404;
}
error_page 404 404.html;
error_page 500 502 503 504 50x.html;
當我啟動我的 nginx 服務器并請求類似的路由https://my_server/blah/時,我被正確重定向到https://my_server/404.html可以看到我的自定義 404 的頁面。但是,如果我向帶有子目錄的 url 發出另一個請求:https://my_server/blah/baz我的 nginx 服務器會嘗試查找https://my_server/blah/404.html
my_server-reverse_proxy-1 | 2022/05/02 23:05:35 [error] 7#7: *1 open() "/usr/share/nginx/html/blah/404.html" failed (2: No such file or directory), client: 11.11.111.111, server: my_server.com, request: "GET /blah/404.html HTTP/1.1", host: "my_server.com"
如何正確配置我的 NGINX 服務器以處理不存在的子目錄 url 的 404?
uj5u.com熱心網友回復:
要從檔案根目錄加載錯誤檔案,它們的相對 URL 應以/
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
error_page的nginx 檔案顯示此代碼帶有前導斜杠。包括它是通常的做法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/471258.html
標籤:http nginx 网址 http-status-code-404
