I'm using nginx like a frontend for an apache server. The config file appears like:

upstream apache {
    server localhost:8000;
}

server {
    listen 80;
    error_page 503 /www/static/503.html;

    # need some magic here #

    location /static/ {
        root /www/static/;
    }

    location / {
        proxy_path http://apache/;
    }
}

For the time being, when apache is lower, I get a plain 502 page produced by nginx. Steps to make it serve my custom error page and return status code 503 that is more relevant in cases like this?

Something similar to this

upstream apache {
    server localhost:8000;
}

server {
    listen 80;
    error_page 502 503 /www/static/503.html;

    location /static/ {
        root /www/static/;
    }

    location / {
        proxy_path http://apache/;
    }
}

You are able to append standard error codes together to show just one page for various kinds errors.
For instance:

error_page 502 503 /www/static/503.html;

For additional reference you are able to refer the error_page manual


Around the error_page manual it states

In addition, you'll be able to change the code of response to another, for example:

error_page 404 =200 /.empty.gif;

An alternative choice
To really make it return another error code you are able to take advantage of the return keyword
For instance:

# check for a condition
  if (condition) {
     return 503;
  }

Also See
nginx: Create HTTP 503 Maintenance Custom Page