【问题标题】:Changing status code from 502 to 503 in fallback route在回退路由中将状态码从 502 更改为 503
【发布时间】:2018-05-21 19:33:51
【问题描述】:

我正在使用 nginx 作为我的项目的代理服务器。如果我的应用因维护而离线,我​​想显示一个后备页面。到目前为止,这工作正常。唯一的问题是,服务器响应 502 错误代码 - 这是有道理的。但是,如何在我的后备中将其更改为 503 路由?

server {
    listen 80;
    error_page 500 502 503 504 @fallback;

   location / {
     proxy_pass http://0.0.0.0:3000;
   }

   location @fallback {
       // I need this to answer with a status of 503 instead of a 502
       root /srv/my-project/static;
       try_files /fallback.html;
   }

}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    可以设置错误页面nginx error page

    并设置类似

    error_page 502 =503 /maintenance.html

    或类似的东西

        location / {
            error_page 502 =503 /maintenance.html;
            include proxy_params;
            proxy_pass http://unix:/var/run/my.sock;
        }
        location /maintenance.html {
            return 503;
        }
    

    来源:How can I make Nginx return HTTP 503 when my proxied app server is down?

    【讨论】:

      【解决方案2】:

      感谢@shalbafzadeh 的回答,为我解决了这个问题。

      在我非常特殊的情况下,解决方案如下所示:

      server {
          listen 80;
          error_page 502 =503 @fallback; <-- THIS
      
         location / {
           proxy_pass http://0.0.0.0:3000;
         }
      
         location @fallback {
             root /srv/my-project/static;
             try_files /fallback.html;
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-08
        • 2019-05-20
        • 1970-01-01
        • 2016-10-21
        • 2020-04-26
        • 2016-02-26
        • 2018-01-05
        • 1970-01-01
        相关资源
        最近更新 更多