Custom 503 Error Page
Douglas A. Seifert
dseifert at searchspark.com
Sun Feb 3 18:51:51 MSK 2008
> > > > Hmm... that doesn't seem to work either. Is it possible that `return xxx;`
> > > > always generates the internal response?
> > > >
> > > > The only thing that would achieve the desired result in this case is
> > > > proxying to some blackhole which would cause the 503 to be caught and
> > > > rewritten according to the error_page directive.
> > >
> > > Oops. Sorry, I missed. The real problem is that with suggested
> > > configuration (i.e. always return 503 at server level) there
> > > is no way to reach /system/maintenance.html file for nginx.
> > >
> > > So, it tries to get /system/maintenance.html for error body, and
> > > gets yet another 503. So it has to return hardcoded content.
> > >
> > > The only solution is to allow nginx to access
> > > /system/maintenance.html somehow. Something like this:
> > >
> > > error_page 503 /system/maintenance.html;
> > >
> > > location / {
> > > if (-f ...) {
> > > return 503;
> > > }
> > > }
> > >
> > > location /system/maintenance.html {
> > > # allow requests here - do not return 503
> > > }
> > >
> > > The if{} block should be in all locations where access should be
> > > disallowed, but not for /system/maintenance.html itself.
> > >
> > > Maxim Dounin
> > >
> >
> > Doing it that way gets me the custom content, but with a 200 OK
> > response. :( I'm afraid the only way I will be able to do this is to
> > recompile nginx with the default 503 content set to what I want it to be
> > (that's my current workaround). Or dig up my extremely rusty C skills
> > and try to figure out the bug, if any.
>
> The way suggested by Maxim should work. Are you sure that yo do not use
>
> error_page 503 = /system/maintenance.html;
>
> instead of
>
> error_page 503 /system/maintenance.html;
>
I had forgotten to pull the rewrite directive out of the if:
I had
if (-f ...) {
rewrite ^(.*)$ /system/maintenance.html last;
return 503;
}
Taking the rewrite directive out fixed it.
For those interested, the final config that does what I want is below.
I imagine a more complicated config will be harder to deal with because
I will have to make sure the check for the maintenance page stays out of
the top level of the config.
test.conf
----------------------------------------
# user and group to run as
#user www www;
# number of nginx workers
worker_processes 6;
# pid of nginx master process
pid /usr/local/www/nginx.pid;
# Number of worker connections. 1024 is a good default
events {
worker_connections 1024;
}
# start the http module where we config http access.
http {
# pull in mime-types. You can break out your config
# into as many include's as you want to make it cleaner
include /usr/local/nginx/conf/mime.types;
# set a default type for the rare situation that
# nothing matches from the mimie-type include
default_type application/octet-stream;
# configure log format
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer"
'
'"$http_user_agent" "$http_x_forwarded_for"';
# main access log
access_log /usr/local/www/log/nginx_access.log main;
# main error log
error_log /usr/local/www/log/nginx_error.log debug;
# no sendfile on OSX
sendfile on;
# These are good default values.
tcp_nopush on;
tcp_nodelay off;
# output compression saves bandwidth
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript
text/xml application/xml application/xml+rss text/javascript;
server {
# port to listen on. Can also be set to an IP:PORT
listen *:8080;
# Set the max size for file uploads to 50Mb
client_max_body_size 50M;
# sets the domain[s] that this vhost server requests for
server_name .foo.com *;
# doc root
root /usr/local/www/test;
# vhost specific access log
access_log /usr/local/www/log/nginx.vhost.access.log main;
# this rewrites all the requests to the maintenance.html
# page if it exists in the doc root. This is for capistrano's
# disable web task
error_page 500 502 504 /500.html;
error_page 503 /system/maintenance.html;
location /system/maintenance.html {
# Allow requests
}
location / {
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
}
}
-----------------------------------------------------
More information about the nginx
mailing list