Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return 503 for POST request in Nginx

I have simple configuration file that is used to server custom 503 error page at a time of maintenance. The relevant part is this:

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;
    }
}

The problem is Nginx figures out that any request resolves in a static file and any POST, PUT and DELETE requests get 405 (method not allowed) response.

So the question is: how do I tell Nginx to serve my page for any HTTP method?

like image 984
cababunga Avatar asked Apr 23 '13 23:04

cababunga


People also ask

Why is Nginx returning 503?

503 Service Temporarily Unavailable Error means that NGINX is unable to handle the request as it is temporarily overloaded or facing resource constraints. It is different from 500 internal server error where the server is simply unable to process your request.

How do I fix 503 service temporarily unavailable Nginx?

Restart Your Server and Networking Equipment The 503 service unavailable error can happen due to connectivity issues between the server chain hosting your application. Thus, if you have root access to your website, one of the easiest ways to fix the 503 service temporarily unavailable error is to reboot your server.

What is Error 503 on Discovery Plus?

The 503 Service Unavailable error is an HTTP status code that means a website's server is not available right now. Most of the time, it occurs because the server is too busy or maintenance is being performed on it.


1 Answers

I ran into this today. It seems the issue is due to nginx (like most servers) not letting you POST to a static file.

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;
}

Source: https://www.onehub.com/blog/2009/03/06/rails-maintenance-pages-done-right/

like image 100
Jay Avatar answered Oct 19 '22 11:10

Jay