Mod_proxy+mod_cache - no upstream & set error_page?


I’m using Nginx with mod_proxy+mod_cache to proxy and cache specific

When the upstream ($host) is down, and the page is not cached, Nginx
returns a generic 502 page. Is it possible to use error_page?

I’ve tried putting error_page 502 directive in multiple places to no

Here is my full config:

user daemon;
worker_processes 5;
worker_rlimit_nofile 20480;

error_log /var/log/nginx/error.log;
pid /var/run/;

events {
worker_connections 4096;
# multi_accept on;
use epoll;

http {
include /etc/nginx/mime.types;

 access_log /var/log/nginx/access.log;

 sendfile on;
 #tcp_nopush on;

 #keepalive_timeout 0;
 keepalive_timeout 65;
 tcp_nodelay on;

 gzip on;
 gzip_disable msie6;
 gzip_proxied any;

 proxy_cache_path /path/to/cache levels=1:1:2

keys_zone=maint_cache:512m inactive=15d max_size=30g;
proxy_hide_header Expires;
proxy_hide_header Cache-Control;

 server {
     listen 80;

     location /nginx_status {
         stub_status on;
         access_log off;
         deny all;

     root /path/to/htdocs;

     log_format cache_log '$time_local '
         '$upstream_cache_status '
         'Cache-Control: $upstream_http_cache_control '
         'Expires: $upstream_http_expires '
         '"$request" ($status) '
         '"$http_user_agent" ';

     access_log /var/log/nginx/proxy.access.log;
     access_log /var/log/nginx/proxy.cache.log cache_log;

     proxy_cache mycache;
     proxy_set_header Host $http_host;
     proxy_cache_use_stale error timeout updating http_500 http_502
         http_503 http_504 http_404;

     error_page 404 /default/index.html;

     if ($host ~* ^([a-z0-9\-]+\.([a-z]+))$) {
         set $host_with_www www.$1;

     if ($host !~* ^([a-z0-9\-]+\.([a-z]+))$) {
         set $host_with_www $host;

     location ~ /purge(/.*) {
         proxy_cache_purge mycache "$host_with_www$1";
         deny all;

     # The initial request MUST respond with HTTP 503.
     location ~ ^/$ {
         error_page 503 /503index;
         return 503;

     location /503index {
         proxy_pass http://$host_with_www/path/to/index.php;
         proxy_cache_key "$host_with_www/index.php";

     # If the CSS request isn't HTTP 200 some browsers don't render
     location /path/to/styles.css {
         proxy_pass http://$host_with_www/path/to/styles.css;
         proxy_cache_key "$host_with_www/styles.css";

     location ~ /.*$ {
         error_page 503 /503index;
         return 503;


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs