Nginx配置錯誤頁面

什麼是404頁面

如果碰巧網站出了問題,或者用戶試圖訪問一個並不存在的頁面時,此時服務器會返回代碼爲404的錯誤信息,此時對應頁面就是404頁面。404頁面的默認內容和具體的服務器有關。如果後臺用的是NGINX服務器,那麼404頁面的內容則爲:

404 Not Found


nginx/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.html

4.測試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 successful

5.重啓nginx

  1. kill -HUP 

     `cat /opt/nginx/nginx.pid `

 

配置文件實例:

……

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

charset  gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_intercept_errors on;

gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone  crawler  $binary_remote_addr  10m;

  #65的配置信息
      server
{
   listen       80;
   server_name  www.65.la 65.la *.65.la;
   index index.html index.htm index.php;
   root  /opt/www/65;
   location ~ .*\.(php|php5)?$
   {     
     #fastcgi_pass  unix:/tmp/php-cgi.sock;
     fastcgi_pass  127.0.0.1:9000;
     fastcgi_index index.php;
     include fcgi.conf;
   }
   error_page  404 = /404.html;

#502 等錯誤可以用同樣的方法來配置。
   error_page   500 502 503 504 = /50x.html;
   ocation = /50x.html {
            root   html;
        }
   log_format  65  ‘$remote_addr – $remote_user [$time_local] "$request" ‘
              ‘$status $body_bytes_sent "$http_referer" ‘
              ‘"$http_user_agent" $http_x_forwarded_for’;
   access_log  /opt/nginx/logs/65.log  65;
}

……

 

 

注意事項:

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錯誤)。用如下兩個不存在的地址去訪問:

http://www.65.la/no 將會調用自定義的404.html

http://www.65.la/notfound:將會調用IE默認的404頁面


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章