nginx环境自定义404页面

1,614次阅读
没有评论

首先打开 nginx.conf 文件, 在 fastcgi_temp_file_write_size 128k; 下面添加
fastcgi_intercept_errors on;

注意, 包括; 号

然后在需要定义的站点的里面添加
error_page 404 = /404.htm;

例如

  • server
  • {
  • listen 80;
  • server_name vps.imcat.in;
  • index index.html index.htm index.php;
  • error_page 404 = /404.htm;
  • include location.conf;
  • root /home/www/logs;
  • }
404 页面需要放在站点的跟目录下
测试配置
/usr/local/nginx/sbin/nginx -t
没错误就重启 nginx
/root/lnmp restart
注意事项:
1. 必须要添加:fastcgi_intercept_errors on; 如果这个选项没有设置,即使创建了 404.html 和配置了 error_page 也没有效果。
2. 不要出于省事或者提高首页权重的目的将首页指定为 404 错误页面,也不要用其它方法跳转到首页。
3. 自定义的 404 页面必须大于 512 字节,否则可能会出现 IE 默认的 404 页面。例如,假设自定义了 404.html, 大小只有 11 个字节(内容为:404 错误)。

正文完
 0
评论(没有评论)
验证码