Nginx设置404有很多用户都会遇到,今天由我来教大家如何设置Nginx的404错误页面。
如果你在Nginx里添加的是虚拟主机,那么你就找到这里:/usr/local/nginx/conf/vhost ,然后在vhost文件夹下面找到你对应域名的conf配置文件。
举例说明:www.moonyasa.com.conf 文件代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| server{listen 80;server_name www.moonyasa.com;index index.html index.htm index.php default.html default.htm default.php;root /home/wwwroot/moon;error_page 404 /404.shtml;include moonyasa.conf;location ~ .*\.(php|php5)?${fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fcgi.conf;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~ .*\.(js|css)?${expires 12h;}access_log off;}server {server_name moonyasa.com;rewrite ^(.*) http://www.moonyasa.com/$1 permanent;} |
你只要在里面添加这一行就行了: error_page 404 /404.shtml;
然后在网站的根目录下添加一个404.shtml的文件就可以了。
这个是我网站上的404错误文件,演示:http://www.moonyasa.com/404
而这一段的代码是可以设置域名重定向的:
1
2
3
4
|
意思是:当访问者在IE浏览器访问 moonyasa.com 的时候,IE浏览自动访问到 www.moonyasa.com
这样做的目的是将 moonyasa.com 的权重都给带有 www 的主域名。
没有评论:
发表评论