Nginx反向代理后配置404页面

关键词:proxy_intercept_errors、fastcgi_intercept_errors
 
在网上搜索404配置,有很多配置文章,但都是关于fastcgi_intercept_errors的,不过对我们的项目不起作用。我们的项目使用nginx做反向代理,通过域名区分不同的网站,配置fastcgi_intercept_errors不生效,需要配置proxy_intercept_errors。以下罗列出两种配置方式,供大家参考。
 

1、fastcgi_intercept_errors 配置方式

(1)创建自己的404.html页面

(2)更改nginx.conf在http定义区域加入: fastcgi_intercept_errors on;

(3)更改nginx.conf(或单独网站配置文件,例如在nginx -> sites-enabled下的站点配置文件 ) 中在server 区域加入: error_page 404 = /404.html 或者 error_page 404 = http://www.xxx.com/404.html

(4)更改后重启nginx,,测试nginx.conf正确性: /opt/nginx/sbin/nginx –t

          #502 等错误可以用同样的方法来配置。
          error_page 500 502 503 504 = /50x.html;

 

注意事项:
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错误)

 

 

2、proxy_intercept_errors配置方式

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

    sendfile        on;
    keepalive_timeout  65;
    #fastcgi_intercept_errors on;
    proxy_intercept_errors on;
    error_page   500 502 503 504 404 403 =
http://www.itcast.cn/;

    upstream cloud.itcast.cn {
        server localhost:9999;
    }
   
    server {
        listen 80;
        server_name cloud.itcast.cn;
       
        location / {
            proxy_pass http://cloud.itcast.cn;
            index index.html index.htm;
        }
    }

 
参考:https://zhidao.baidu.com/question/1605046558053306027.html
 
 
 
 
 
 
posted @ 2017-04-21 19:28  春江师兄  阅读(21604)  评论(0编辑  收藏  举报