I'd like to have nginx reverse proxy return 502 on demand if I have emergency task that requires my upstream service to be running, yet users shouldn't connect to it and nginx should indicate "server offline". There's this question that sets up nginx to return an error, but it's harcoded in the config.
Is there a way to set up the config so that I can write a service nginx start
command with some additional parameter that will put /
into an error response?
Best Answer
Our approach: Test if maintenance file exists. If the file is present Nginx responds with HTTP 503 and shows a customized static error page.
This example shows this in conclusion with the PHP/FastCGI gateway. You can use it as well for other local or proxied services.
Keep in mind that Nginx is able to cache the file state if configured accordingly. If so, the server needs some time to react after you have created or deleted the file.
Simple way to activate:
or deactivate the maintenance: