Proxying and caching a fragile server to ensure availability

cachito nginx-forum at nginx.us
Thu Aug 22 01:42:38 UTC 2013


Hello colleagues. I'm trying to save a website hosted on a (VERY)
low-powered server by placing a strong nginx server in front of it as a
caching proxy.

I don't control the website code and the rest of its configuration, so I'm
not free to move it elsewhere stronger.

I'd like to have the caching proxy serve content as fresh as possible,
regardless of the response given by the upstream. If the upstream doesn't
respond, grab whatever we have in cache and serve it.

Below is the configuration for this particular site. With this
configuration, (and I tried taking away many of the cache busting parts
while testing, same results), I'm getting lots of "504 Gateway Timeout"
errors, even after successfully loading a page and just reloading it in the
browser.
Is there something in this setup that is inherently bad?
The server is working perfectly for other sites being proxied, it operates
as an "origin pull CDN" to unload static files from various Wordpress
blogs.

Whatever you could find to help me will be very welcome.

Thanks in advance.

server {
  listen 80;
  server_name .thissite.com;
  access_log /var/log/nginx/thissite.access.log;
  error_log /var/log/nginx/thissite.errors.log; # this log only lists
"upstream timeout" errors, no further details.

  proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
  # Set proxy headers for the passthrough
  #proxy_set_header X-Real-IP $remote_addr;
  #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $host;

  # Let the Set-Cookie and Cache-Control headers through.
  proxy_pass_header Set-Cookie;
  #proxy_pass_header Cache-Control;
  #proxy_pass_header Expires;
  proxy_pass_header Host;

  # Fallback to stale cache on certain errors.
  # 503 is deliberately missing, if we're down for maintenance
  # we want the page to display.
  proxy_cache_use_stale error
                        timeout
                        invalid_header
                        updating
                        http_500
                        http_502
                        http_504
                        http_404;

  # Set the proxy cache key
  set $cache_key $scheme$host$uri$is_args$args;

  #proxy_cache start
  set $no_cache 0;

  # POST requests and urls with a query string should always go to PHP
  if ($request_method = POST) {
    set $no_cache 1;
  }

  if ($query_string != "") {
    set $no_cache 1;
  }

  if ($http_cookie ~*
"comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
    set $no_cache 1;
  }

#The different cache zones reside in different filesystems. the "pages" zone
is saved in RAM for fast delivery.
  location / {
    proxy_pass http://upstream;
    proxy_cache pages;
    proxy_cache_key $cache_key;
    proxy_cache_valid 60m; # I was hoping to have the proxy query the
upstream once every hour for updated content.
    # 2 rules to dedicate the no caching rule for logged in users.
    proxy_cache_bypass $no_cache; # Do not cache the response.
    proxy_no_cache $no_cache; # Do not serve response from cache.
    add_header X-Cache $upstream_cache_status;
    expires 60m;
  }

  location ~* \.(png|jpg|jpeg|gif|ico|swf|flv|mov|mpg|mp3)$ {
    expires max;
    log_not_found off;
    proxy_pass http://upstream;
    proxy_cache images;
    proxy_cache_key $cache_key;
    proxy_cache_valid 365d;
  }

  location ~* \.(css|js|html|htm)$ {
    expires 7d;
    log_not_found off;
    proxy_pass http://upstream;
    proxy_cache scripts;
    proxy_cache_key $cache_key;
    proxy_cache_valid 7d;
  }
}

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,242106,242106#msg-242106



More information about the nginx mailing list