
我正在嘗試為我的 error_page 建立後備。基本上,邏輯應該類似以下:
- 加載 foobar.html
- 遠端伺服器上不存在 -> 從遠端伺服器載入 404.html 以顯示 404 頁面
- 遠端伺服器上不存在 -> 在本機檔案系統上載入 404.html
加載兩者localhost/404.html
並localhost/global404.html
工作,但是當我中斷localhost/404.html
(通過從http伺服器中刪除文件)時,它不會global404.html
按照我的預期顯示頁面。
server {
listen 80;
server_name example.com www.example.com;
proxy_intercept_errors on;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
error_page 404 /404.html;
}
location /404.html {
proxy_pass http://localhost:3000/404.html;
error_page 404 /global404.html;
}
location /global404.html {
root /usr/share/nginx/html;
}
}
當我點擊時,上面的工作正常http://localhost/404.html
(當 404.html 檔案位於遠端伺服器上時,它表明,當我刪除該檔案時,它會載入 global404.html 檔案)。
然而,當我輸入一個不存在的頁面時,我只會得到預設的 nginx 404 頁面。
答案1
由於問題上留下的評論的措辭,我設法找到了recursive_error_pages
允許級聯/遞歸的選項error_pages
。我覺得文檔中遺漏了它很愚蠢。
但是,簡單地做
server {
listen 80;
server_name example.com www.example.com;
proxy_intercept_errors on;
recursive_error_pages on;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
error_page 404 /404.html;
}
location /404.html {
proxy_pass http://localhost:3000/404.html;
error_page 404 /global404.html;
}
location /global404.html {
root /usr/share/nginx/html;
}
}
發揮了魅力。我喜歡 nginx。