nginx 不为我的 error_page 提供服务 [英] nginx not serving my error_page

查看:15
本文介绍了nginx 不为我的 error_page 提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Sinatra application hosted with Unicorn, and nginx in front of it. When the Sinatra application errors out (returns 500), I'd like to serve a static page, rather than the default "Internal Server Error". I have the following nginx configuration:

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,我收到的错误页面仍然是相同的内部服务器错误".

The error_page directive is there, and I have sudo'd as www-data (Ubuntu) and verified I can cat the file, thus it's not a permission problem. With the above config file, and service nginx reload, the page I receive on error is still the same "Internal Server Error".

我的错误是什么?

推荐答案

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

error_page handles errors that are generated by nginx. By default, nginx will return whatever the proxy server returns regardless of http status code.

您要查找的是proxy_intercept_errors

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

This directive decides if nginx will intercept responses with HTTP status codes of 400 and higher.

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

By default all responses will be sent as-is from the proxied server.

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

If you set this to on then nginx will intercept status codes that are explicitly handled by an error_page directive. Responses with status codes that do not match an error_page directive will be sent as-is from the proxied server.

这篇关于nginx 不为我的 error_page 提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆