Return 503 for POST request in Nginx

I have a simple configuration file that is used for server page 503 errors during maintenance. The relevant part is as follows:

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 that Nginx finds out that any request is allowed in a static file, and any POST, PUT and DELETE requests receive a 405 response (method not allowed).

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

+6
source share
2 answers

I came across this today. It seems the problem is with nginx (like most servers), not allowing you a POST static file.

The solution is to capture 405 errors in your @ 503 location block serving the maintenance page. In addition, you will need to enable @recursiveerrorpages @, since you are the first to intentionally drop 503, and then the user drops 405, sending 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/

+3
source

Perhaps try to get 405 requests to be the actual URI:

 error_page 405 = $uri; 
0
source

Source: https://habr.com/ru/post/943491/


All Articles