Rootop 服务器运维与web架构

nginx中关于error_page配置参数介绍

# 当访问文件不存在返回404状态码,由nginx内部重定向返回/404.html的内容。

server {    
.

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html; # 支持多个状态码重定向到同一个地址。
.
}

# 如果写一个外部地址,则会发生302跳转。

server {    
.

    error_page 404 https://www.rootop.org;
    
    #error_page 404 = @fallback; # 这里也可以写内部重定向(location @name 命名位置)
    #location @fallback {
    #    proxy_pass http://backend;
    #}
.
}

# 关于location @name 资料看官方:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
The “@” prefix defines a named location.
Such a location is not used for a regular request processing, but instead used for request redirection.
They cannot be nested, and cannot contain nested locations.

#当nginx配置为FastCGI(如lnmp)发生500错误时进行跳转的时候添加如下:

error_page 500 https://www.rootop.org/500.jpg;

会发现浏览器还是直接显示了500错误,并没有跳转到500.jpg(302)。

这就需要在fastcgi部分添加一行配置 fastcgi_intercept_errors on;

location ~ .*\.(php|php5)?$
{
    try_files $uri =404;
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    fastcgi_intercept_errors on;
}

参数作用参考:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_intercept_errors
意思就是fastcgi服务器发生大于等于300的错误码时,交由nginx的error_page指令处理后返给客户端。
所以在lnmp环境中,如果配置了error_page指令没有效果,就去查有没有这个参数。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/4345.html

作者:Venus

服务器运维与性能优化

评论已关闭。