加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

在Nginx中返回503以获取POST请求

发布时间:2020-12-13 21:32:52 所属栏目:Nginx 来源:网络整理
导读:我有一个简单的配置文件,用于维护时自定义503错误页面.相关部分是这样的: server { listen 80 default; root /usr/share/nginx/html; server_name example.com; location / { if (-f $document_root/503.json) { return 503; } } # error 503 redirect to 5

我有一个简单的配置文件,用于维护时自定义503错误页面.相关部分是这样的:

server {
    listen      80 default;
    root        /usr/share/nginx/html;
    server_name example.com;

    location / {
        if (-f $document_root/503.json) {
            return 503;
        }
    }

    # error 503 redirect to 503.json
    error_page 503 @maintenance;
    location @maintenance {
        rewrite ^(.*)$/503.json break;
    }
}

问题是Nginx发现任何请求都在静态文件中解析,任何POST,PUT和DELETE请求都得到405(方法不允许)响应.

所以问题是:如何告诉Nginx为任何HTTP方法提供页面服务?

最佳答案
我今天碰到了这个.似乎问题是由于nginx(像大多数服务器一样)不让你POST到静态文件.

The solution is to capture 405 errors in your @503 location block,serving
the maintenance page. In addition,you will have to enable
@recursiveerrorpages@,since you are first,intentionally,throwing a
503 error,and then the user is throwing a 405 by posting to your
static file:

recursive_error_pages on;

if (-f $document_root/system/maintenance.html) {
  return 503;
}

error_page 404 /404.html;
error_page 500 502 504 /500.html;
error_page 503 @503;
location @503 {

  error_page 405 = /system/maintenance.html;

  # Serve static assets if found.
  if (-f $request_filename) {
    break;
  }

  rewrite ^(.*)$/system/maintenance.html break;
}

资料来源:https://www.onehub.com/blog/2009/03/06/rails-maintenance-pages-done-right/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读