Heim  >  Artikel  >  Backend-Entwicklung  >  NGINX 配置404错误页面转向

NGINX 配置404错误页面转向

WBOY
WBOYOriginal
2016-07-25 08:46:49995Durchsuche
什么是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
kill -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必须和评论者名相匹配,区分大小 写
NGINX 配置404错误页面转向


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn