Forum: NGINX Proxying and caching a fragile server to ensure availability

2974d09ac2541e892966b762aad84943?d=identicon&s=25 cachito (Guest)
on 2013-08-22 03:43
(Received via mailing list)
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
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2013-08-22 04:25
(Received via mailing list)
Hello,

On Wed, Aug 21, 2013 at 9:42 PM, cachito <nginx-forum@nginx.us> wrote:

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

​​If the user sends requests using cookies from Wordpress, the cache
won't
be used...
Thus, you seem not to cache requests from Wordpress at all. That CMS
need
resources such as power and generates huge traffic (that needs to be
processed aswell).
Are you sure not all the Wordpress requests contain cookie(s)?
​


> #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.
>

​​Read the correct syntax and use of proxy_cache_valid.
If no HTTP code is specified as optional parameter, it says 'then only
200,
301 and 302 responses are cached'.
​


>     # 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.
>

​​Actually, those 2 comments are inverted: proxy_no_cache controls
whether
or not items should be written in cache and proxy_cache_bypass controls
whether or not content should be read from it.
​


>     add_header X-Cache $upstream_cache_status;
>     expires 60m;
>   }
>
---
*B. R.*
2974d09ac2541e892966b762aad84943?d=identicon&s=25 cachito (Guest)
on 2013-08-22 04:36
(Received via mailing list)
B.R. Wrote:
-------------------------------------------------------
> >
>
> ​​If the user sends requests using cookies from Wordpress, the cache
> won't
> be used...
> Thus, you seem not to cache requests from Wordpress at all. That CMS
> need
> resources such as power and generates huge traffic (that needs to be
> processed aswell).
> Are you sure not all the Wordpress requests contain cookie(s)?
> ​

I'm manually deleting cookies to force caching and it isn't happening.
Even
if I set $no_cache to 0 in this particular section (or at the end of the
if
list) nothing happens.

> >     proxy_cache_valid 60m; # I was hoping to have the proxy query
> the
> > upstream once every hour for updated content.
> >
>
> ​​Read the correct syntax and use of proxy_cache_valid.
> If no HTTP code is specified as optional parameter, it says 'then only
> 200,
> 301 and 302 responses are cached'.

This is the intended behavior. If the upstream responds something
useful,
cache it and serve it. Errors shouldn't be cached, and the stale cache
should be served for that URL.

> or not items should be written in cache and proxy_cache_bypass
> controls
> whether or not content should be read from it.

Comments aside, when $no_cache is set to 0 it mandates that the upstream
response should be cached and the client should be served content from
cache, right?


Thanks B. R.
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,242106,242110#msg-242110
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2013-08-22 04:54
(Received via mailing list)
Hello,


On Wed, Aug 21, 2013 at 10:35 PM, cachito <nginx-forum@nginx.us> wrote:

> > >     set $no_cache 1;
> > Are you sure not all the Wordpress requests contain cookie(s)?
> > ​
>
> I'm manually deleting cookies to force caching and it isn't happening. Even
> if I set $no_cache to 0 in this particular section (or at the end of the if
> list) nothing happens.
>

​Is your conf really applied? Check logs on reload to be sure.
​


> > >     proxy_cache_key $cache_key;
> This is the intended behavior. If the upstream responds something useful,
> cache it and serve it. Errors shouldn't be cached, and the stale cache
> should be served for that URL.
>

​Ok​



> > or not items should be written in cache and proxy_cache_bypass
> > controls
> > whether or not content should be read from it.
>
> Comments aside, when $no_cache is set to 0 it mandates that the upstream
> response should be cached and the client should be served content from
> cache, right?
>

I unnderstood the double negation the same way as yours.
---
*B. R.*
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.