During website maintenance, it is sometimes necessary to shutdown our website.
Our current method is to touch
a file which will trigger the web server (nginx) to redirect traffic to a maintenance page hosted in Amazon S3. It is important to host the maintenance page on a external server because during maintenance there can be no guarantee about the availability of any local files.
This is the nginx config that we use for "Maintenance Mode":
server {
...
# Redirect processing of 503 error pages into a named location:
error_page 503 @maintenance;
# "Maintenance Mode" is off by default - Use a nginx variable to track state.
set $maintenance off;
# Swith on "Maintenace Mode" if a certain file exists.
if (-f /var/www/mysite/shared/maintenanceON) {
set $maintenance on;
}
# Don't use "Maintenance Mode" for our own offices.
if ($remote_addr ~ (69.69.96.69|69.69.69.79)) {
set $maintenance off;
}
# Don't use "Maintenance Mode" for certain urls, e.g. the load-balancer ping page.
if ($uri ~ ^/(site\/ping|robots\.txt)$) {
set $maintenance off;
}
if ($maintenance = on) {
return 503; # 503 - Service unavailable
}
location @maintenance {
# Redirect the request to our maintenance page in Amazon S3.
rewrite ^(.*)$ http://mysite.s3-website-us-east-1.amazonaws.com/ break;
}
...
It works great. But there is an unfortunate side-effect I wondered whether is avoidable?
The rewrite
is done by Nginx using a 302 http status code to forward the request to the Amazon S3 site. Therefore, during Maintenance mode, we are not returning a 503, we are returning a 302. This isn't good etiquette and could be bad if google bot crawled us during a planned site dowmtime period. Google recommends a 503 (source).
Is there a nginx directive I can use to get the same effect but without a 302 for the redirect?
This shows maintenance mode returns a "302 Moved Temporarily" rather than the "503 Service unavailable" that I'd prefer:
> wget http://staging.example.com
--2013-11-13 10:00:47-- http://staging.example.com/
Resolving staging.example.com (staging.example.com)... 69.69.69.80
Connecting to staging.example.com (staging.example.com)|69.69.69.80|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://example.s3-website-us-east-1.amazonaws.com/ [following]
--2013-11-13 10:00:53-- http://example.s3-website-us-east-1.amazonaws.com/
Resolving example.s3-website-us-east-1.amazonaws.com (example.s3-website-us-east-1.amazonaws.com)...