Nginx反向代理后配置404頁面


關鍵詞:proxy_intercept_errors、fastcgi_intercept_errors
 
在網上搜索404配置,有很多配置文章,但都是關於fastcgi_intercept_errors的,不過對我們的項目不起作用。我們的項目使用nginx做反向代理,通過域名區分不同的網站,配置fastcgi_intercept_errors不生效,需要配置proxy_intercept_errors。以下羅列出兩種配置方式,供大家參考。
 

1、fastcgi_intercept_errors 配置方式

(1)創建自己的404.html頁面

(2)更改nginx.conf在http定義區域加入: fastcgi_intercept_errors on;

(3)更改nginx.conf(或單獨網站配置文件,例如在nginx -> sites-enabled下的站點配置文件 ) 中在server 區域加入: error_page 404 = /404.html 或者 error_page 404 = http://www.xxx.com/404.html

(4)更改后重啟nginx,,測試nginx.conf正確性: /opt/nginx/sbin/nginx –t

          #502 等錯誤可以用同樣的方法來配置。
          error_page 500 502 503 504 = /50x.html;

 

注意事項:
1、必須要添加:fastcgi_intercept_errors on; 如果這個選項沒有設置,即使創建了404.html和配置了error_page也沒有效果。fastcgi_intercept_errors 語法: fastcgi_intercept_errors on|off 默認: fastcgi_intercept_errors off 添加位置: http, server, location 默認情況下,nginx不支持自定義404錯誤頁面,只有這個指令被設置為on,nginx才支持將404錯誤重定向。這里需要注意的是,並不是說設置了 fastcgi_intercept_errors on,nginx就會將404錯誤重定向。在nginx中404錯誤重定向生效的前提是設置了fastcgi_intercept_errors on,並且正確的設置了error_page這個選項(包括語法和對應的404頁面)
2、不要出於省事或者提高首頁權重的目的將首頁指定為404錯誤頁面,也不要用其它方法跳轉到首頁。
3、自定義的404頁面必須大於512字節,否則可能會出現IE默認的404頁面。例如,假設自定義了404.html,大小只有11個字節(內容為:404錯誤)

 

 

2、proxy_intercept_errors配置方式

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    #fastcgi_intercept_errors on;
    proxy_intercept_errors on;
    error_page   500 502 503 504 404 403 =
http://www.itcast.cn/;

    upstream cloud.itcast.cn {
        server localhost:9999;
    }
   
    server {
        listen 80;
        server_name cloud.itcast.cn;
       
        location / {
            proxy_pass http://cloud.itcast.cn;
            index index.html index.htm;
        }
    }

 
參考:https://zhidao.baidu.com/question/1605046558053306027.html
 
 
 
 
 
 


免責聲明!

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



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