【问题标题】:nginx not serving my error_pagenginx 没有为我的 error_page 提供服务
【发布时间】:2012-02-01 15:47:14
【问题描述】:

我有一个由 Unicorn 托管的 Sinatra 应用程序,前面是 nginx。当 Sinatra 应用程序出错(返回 500)时,我想提供一个静态页面,而不是默认的“内部服务器错误”。我有以下 nginx 配置:

server {
  listen 80 default;
  server_name *.example.com;
  root /home/deploy/www-frontend/current/public;

  location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 5;
    proxy_read_timeout 240;
    proxy_pass http://127.0.0.1:4701/;
  }

  error_page 500 502 503 504 /50x.html;
}

error_page 指令在那里,我已将 sudo 设置为 www-data (Ubuntu) 并验证我可以cat 文件,因此这不是权限问题。使用上述配置文件和service nginx reload,我收到的错误页面仍然是相同的“内部服务器错误”。

我的错误是什么?

【问题讨论】:

    标签: nginx custom-error-pages


    【解决方案1】:

    error_page 处理 nginx 生成的错误。默认情况下,无论http状态码如何,nginx都会返回代理服务器返回的任何内容。

    你要找的是proxy_intercept_errors

    该指令决定 nginx 是否会拦截带有 HTTP 的响应 400 及更高的状态代码。

    默认情况下,所有响应都将从代理服务器按原样发送。

    如果您将其设置为 on,则 nginx 将拦截以下状态代码 由 error_page 指令显式处理。带有状态的响应 与 error_page 指令不匹配的代码将按原样发送 来自代理服务器。

    【讨论】:

    • 我知道这是 RTFM 的问题。感谢您花时间提供一个很好的答案!
    • 只是对 4 年前的回复的快速说明 - 现在 proxy_intercept_errors 适用于等于或大于 300 的错误。
    • 设置此指令后,自定义错误页面被启用,但从上游发送的自定义响应标头被丢弃。任何想法如何解决这部分?
    【解决方案2】:

    您可以特别针对该位置设置 proxy_intercept_errors

    location /some/location {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 5;
        proxy_read_timeout 240;
        proxy_pass http://127.0.0.1:4701/;
        proxy_intercept_errors on; # see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
    
        error_page 400 500 404 ... other statuses ... =200 /your/path/for/custom/errors;
    }
    

    你可以设置 200 个其他状态,而不是你需要的

    【讨论】:

    • 使用proxy_intercept_errors;(不带参数)在当前 nginx 中不再有效。请改用proxy_intercept_errors on;
    • 这里关于语义的快速问题...设置proxy_intercept_errorson表示返回自定义页面,设置为off表示返回nginx页面,对吗?跨度>
    • 我正在使用 FASTCGI,即使我打开 proxy_intercept_errors 仍然不会拦截错误。它甚至不会拦截 404 错误。
    【解决方案3】:

    使用 FastCGI 作为上游的人需要打开此参数

    fastcgi_intercept_errors on;
    

    对于我的 PHP 应用程序,我在上游配置块中使用它

     location ~ .php$ { ## Execute PHP scripts
        fastcgi_pass   php-upstream; 
        fastcgi_intercept_errors on;
        error_page 500 /500.html;
     }
    

    【讨论】:

      【解决方案4】:

      正如斯蒂芬in this response 所提到的,使用proxy_intercept_errors on; 可以工作。 虽然在我的情况下,如 in this answer 所见,使用 uwsgi_intercept_errors on; 就可以了......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多