NGINX 配置404錯誤頁面轉向

來源:互聯網
上載者:User
什麼是404頁面如果碰巧網站出了問題,或者使用者試圖訪問一個並不存在的頁面時,此時伺服器會傳回碼為404的錯誤資訊,此時對應頁面就是404頁面。404頁面 的預設內容和具體的伺服器有關。如果後台用的是NGINX伺服器,那麼404頁面的內容則為:404 Not Foundnginx/0.8.6為什麼要自訂404頁面在訪問時遇到上面這樣的404錯誤頁面,我想99%(未經調查,估計資料)的使用者會把頁面關掉,使用者就這樣悄悄的流失了。如果此時能有一個漂亮的頁 面能夠引導使用者去他想去的地方必然可以留住使用者。因此,每一個網站都應該自訂自己的404頁面。NGINX下如何自訂404頁面IIS和APACHE下自訂404頁面的經驗介紹文章已經非常多了,NGINX的目前還比較少,湊巧我的幾台伺服器都是NGINX的,為瞭解決自 家的問題特地對此作了深入的研究。研究結果表明,NGINX下配置自訂的404頁面是可行的,而且很簡單,只需如下幾步:1.建立自己的404.html頁面2.更改nginx.conf在http定義地區加入:fastcgi_intercept_errors on;3.更改nginx.conf在server 地區加入:error_page 404 = /404.html4.測試nginx.conf正確性:/opt/nginx/sbin/nginx –t如果正確應該顯示如下資訊:the configuration file /opt/nginx/conf/nginx.conf syntax is ok
configuration file /opt/nginx/conf/nginx.conf test is successfulkill -HUP `cat /opt/nginx/nginx.pid `設定檔執行個體:……
  1. http
  2. {
  3. include mime.types;
  4. default_type application/octet-stream;
  5. charset gb2312;
  6. server_names_hash_bucket_size 128;
  7. client_header_buffer_size 32k;
  8. large_client_header_buffers 4 32k;
  9. client_max_body_size 8m;
  10. sendfile on;
  11. tcp_nopush on;
  12. keepalive_timeout 60;
  13. tcp_nodelay on;
  14. fastcgi_connect_timeout 300;
  15. fastcgi_send_timeout 300;
  16. fastcgi_read_timeout 300;
  17. fastcgi_buffer_size 64k;
  18. fastcgi_buffers 4 64k;
  19. fastcgi_busy_buffers_size 128k;
  20. fastcgi_temp_file_write_size 128k;
  21. fastcgi_intercept_errors on;
  22. gzip on;
  23. gzip_min_length 1k;
  24. gzip_buffers 4 16k;
  25. gzip_http_version 1.0;
  26. gzip_comp_level 2;
  27. gzip_types text/plain application/x-javascript text/css application/xml;
  28. gzip_vary on;
  29. #limit_zone crawler $binary_remote_addr 10m;
複製代碼
#65的配置資訊
  1. server
  2. {
  3. listen 80;
  4. server_name www.65.la 65.la *.65.la;
  5. index index.html index.htm index.php;
  6. root /opt/www/65;
  7. location ~ .*\.(php|php5)?$
  8. {
  9. #fastcgi_pass unix:/tmp/php-cgi.sock;
  10. fastcgi_pass 127.0.0.1:9000;
  11. fastcgi_index index.php;
  12. include fcgi.conf;
  13. }
  14. error_page 404 = /404.html;
複製代碼
#502 等錯誤可以用同樣的方法來配置。
  1. error_page 500 502 503 504 = /50x.html;
  2. ocation = /50x.html {
  3. root html;
  4. }
  5. log_format 65 ‘$remote_addr – $remote_user [$time_local] "$request" ‘
  6. ‘$status $body_bytes_sent "$http_referer" ‘
  7. ‘"$http_user_agent" $http_x_forwarded_for’;
  8. access_log /opt/nginx/logs/65.log 65;
  9. }
複製代碼
……注意事項: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錯誤)。用如下兩個不存在的地址去訪問:
問一個問題,建立的404頁面放在哪兒呢?
我還沒去嘗試
@5169.info
放在哪裡都可以,注意改下這行配置就可以了
error_page 404 = /404.html;

@5169.info
放在哪裡都可以,注意改下這行配置就可以了
error_page 404 = /404.html;也就是說,每個子站都複製一份404檔案,可不可以這樣
error_page 404 = ../404.html;
只做一套 404放在htdocs下面
本文目前尚無任何 trackbacks 和 pingbacks.
注 意: 評論者允許使用'@user:'的方式將自己的評論通知另外評論者。
例如, ABC是本文的評論者之一,則使用'@ABC不包括單引號)將會自動將您的評論發送給ABC。
user必須和評論者名相匹配,區分大小 寫

start.jpg (412.91 KB, 下載次數: 35)

下載附件

404

2015-7-19 14:55 上傳


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.