Custom 503 Error Page

Douglas A. Seifert dseifert at
Sun Feb 3 02:18:27 MSK 2008

I am trying to use a test for the existence of a file to return a error
page with a 503 Temporarily Unavailable response code.  My configuration
is below.  The problem is that it does not work.  I can see the custom
error page, but the HTTP status code is 200, not the desired 503.

If I change the if directive  to this:

    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html;  # No last
      return 503;

I start getting a 503 HTTP status code, but the content is not my custom
error page, but rather the default 503 response compiled into the nginx

Am I doing something terribly wrong?  I would really like to see my
custom page with a real 503 HTTP status code.

Thanks for any help,
Douglas A. Seifert

# 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/;

# 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 *;

    # 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
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      return 503;

    location / {
      root /usr/local/www/test;

    error_page   500 502 504  /500.html;
    error_page   503 /503.html;


More information about the nginx mailing list