Nginx 自定義404 頁面


主要是記錄踩過的一個坑。。。

nginx要自定義404和401的頁面,但是error_page 配置沒有生效,沒有正常跳轉。

 error_page 404  /404.html;

error_page 404 = http://www.test.com/error.html;

http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#error_page

這是因為我們的404靜態資源在上游服務器上,而不是當前nginx直接提供

nginx proxy 啟用自定義錯誤頁面:

語法:proxy_intercept_errors on | off;

默認值:

proxy_intercept_errors off;

上下文:http, server, location

當被代理的后端服務器的響應狀態碼大於等於300時,決定是否直接將響應發送給客戶端,亦或將響應轉發給nginx由error_page指令來處理。

 proxy_intercept_errors 為on 表示 nginx按照原response code 輸出,后端是404就是404。這個變量開啟后,我們才能自定義錯誤頁面。

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors

總結幾種404 自定義頁面的配置:

第一種:Nginx自己的錯誤頁面

看下面的配置,這種情況下不需要修改任何參數,就能實現這個功能。

server {

    listen      80;

    server_name  www.test.com;

    root   /data/wwwroot/test;

   index  index.html index.htm;

    location / {

    }

    # 定義錯誤頁面碼,如果出現相應的錯誤頁面碼,轉發到那里。

    error_page  404 403 500 502 503 504  /404.html;

    # 承接上面的location。

    location = /404.html {

    # 放錯誤頁面的目錄路徑。當然默認可以在網站目錄下,也可以定義放置錯誤頁面的位置。

        root   /usr/share/nginx/html;

    }

}

第二種:反向代理的錯誤頁面

如果后台靜態資源處理報錯拋出404,想把這個狀態叫Nginx反饋給客戶端或者重定向到某個連接,配置如下:

upstream www {

    server 192.168.1.88:80  weight=20 max_fails=2 fail_timeout=30s;

}

server {

    listen       80;

    server_name www.test.com;

    root   /data/wwwroot/test;

    index  index.html index.htm;

     location / {

        if ($request_uri ~* '^/$') {

                    rewrite .*   http://www.test.com/index.html redirect;

        }

        # 關鍵參數:這個變量開啟后,我們才能自定義錯誤頁面,當后端返回404,nginx攔截錯誤定義錯誤頁面

        proxy_intercept_errors on;

        proxy_pass      http://www;

        proxy_set_header HOST   $host;

        proxy_set_header X-Real-IP      $remote_addr;

        proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for;

    }

    error_page    404  /404.html;

    location = /404.html {

        root   /usr/share/nginx/html;

    }

}

第三種:Nginx解析php代碼的錯誤頁面

如果后端是php解析的,需要加一個變量

在http段中加一個變量 fastcgi_intercept_errors on 就可以了。

指定一個錯誤頁面:

error_page    404  /404.html;

location = /404.html {

    root   /usr/share/nginx/html;

}

注意事項:

1.必須要添加:fastcgi_intercept_errors on; 如果這個選項沒有設置,即使創建了404.html和配置了error_page也沒有效果。

fastcgi_intercept_errors 語法:

fastcgi_intercept_errors on|off

默認: fastcgi_intercept_errors off

當FastCGI后端服務器響應狀態碼大於等於300時,決定是否直接將響應發送給后端客戶端,或者將響應轉發給nginx由 error_page指令來處理。

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_intercept_errors

fastcgi_intercept_errors on表示接收fastcgi輸出的http 1.0 response code,后端php可以輸出header指示nginx輸出什么錯誤頁面。開啟這個之后,我們才能在php里面自定義錯誤代碼和頁面。

 添加位置: http, server, location

默認情況下,nginx不支持自定義404錯誤頁面,只有這個指令被設置為on,nginx才支持將404錯誤重定向。

這里需要注意的是,並不是說設置了on,nginx就會將404錯誤重定向。

在nginx中404錯誤重定向生效的前提是設置了fastcgi_intercept_errors on,並且正確的設置了error_page這個選項(包括語法和對應的404頁面)

2.不要出於省事或者提高首頁權重的目的將首頁指定為404錯誤頁面,也不要用其它方法跳轉到首頁。

3.自定義的404頁面必須大於512字節,否則可能會出現IE默認的404頁面。例如,假設自定義了404.html,大小只有11個字節(內容為:404錯誤)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM