2014-04-13 15:05:01
来 源
IT技术网
Nginx
本文介绍如何在Apache,iis和Nginx服务器设置404错误页面,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
Apache/ <wbr>IIS/.net以及Nginx设置404页面的方法

一、 Apache下设置404错误页面(一般是Linux主机)

为Apache Server设置 404错误页面的方法很简单,只需:

(1)在.htaccess 文件中加入如下内容:ErrorDocument 404 /404.php,将.htaccess文件上传到网站根目录

(2)制作一个404页面,随便您设计,命名为404.php,同样上传到网站根目录。

二、 IIS/.net下设置404错误页面

现在的idc提供商基本都提供404设置的功能,直接上传文件设置即可。在IIs中设置方法:打开IIS管理器–>点击要设置自定义404的网站的属性–>点击自定义错误选项–>选中404页–>选中并打开编辑属性–>设置成 URL –> URL 里填写“/404.html”–>按确定退出再把做好的404.html 页面上传到网站根目录下。此处在“消息类型”中一定要选择“文件”或“默认值”,而不要选择“URL”,不然,将导致返回“200”状态码。

三、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 `

配置文件实例:

……

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错误)。

Apache/ <wbr>IIS/.net以及Nginx设置404页面的方法

404制作设置注意事项:

1.不要将404错误转向到网站主页,否则可能会导致主页在搜索引擎中消失

2.不要使用绝对URL,如果使用绝对URL返回的状态码是302+200

3.404页面设置完成,一定要检查是否正确。但http头信息返回的一定要是404状态。这主要是对搜索引擎有关系,因为如果你网站产生较多页面时候但搜索引擎看到的是很多一样的正常页面,有可能会误被认为作弊。

4.404页面不要自动跳转,让用户来决定去向。这涉及到404页面的制作,提供用户体验很重要,404页面制作很有学问。

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。