# Weird caching behaviour

**URL:** <https://www.ruby-forum.com/t/weird-caching-behaviour/217097>\
**Category:** NGINX\
**Created:** [February 21, 2012, 3:29pm UTC](https://www.ruby-forum.com/t/weird-caching-behaviour/217097 "2012-02-21T15:29:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 21, 2012, 3:29pm UTC](https://www.ruby-forum.com/t/weird-caching-behaviour/217097/1 "2012-02-21T15:29:02Z")

</div>

Hi guys,

we’re running a load balanced cluster with nginx as load balancing  
software  
and use the caching feature. So far we’re caching for 3 high frequent  
sites  
and it’s working great.  
Now when I add another site to be cached (configuration is below) nginx  
starts to cache EVERY website that it’s loadbalancing. Can you find an  
error  
in the configuration or tell me why it’s doing this? Thanks in advance.

Nginx.conf:  
user nginx nginx;  
worker\_processes 4;  
worker\_cpu\_affinity 0001 0010 0100 1000;

error\_log /var/log/nginx/error\_log info;

events {  
worker\_connections 1024;  
use epoll;  
}

http {  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;

# standard logging

log\_format main  
'$remote\_addr - $remote\_user [$time\_local] ’  
'“$request” $status $bytes\_sent ’  
'“$http\_referer” “$http\_user\_agent” ’  
‘“$gzip\_ratio”’;

# cache logging

```
    log_format cache '$remote_addr - $remote_user [$time_local] -

```

$http\_referer - ’  
'$upstream\_cache\_status ’  
'Cache-Control: $upstream\_http\_cache\_control ’  
'Expires: $upstream\_http\_expires ’  
'“$request” ($status) ’  
'“$http\_user\_agent” ';  
access\_log /var/log/nginx/cache.log cache;

client\_header\_timeout 10m;  
client\_body\_timeout 10m;  
send\_timeout 10m;

connection\_pool\_size 256;  
client\_header\_buffer\_size 1k;  
large\_client\_header\_buffers 4 8k;  
request\_pool\_size 4k;  
client\_max\_body\_size 100M;

gzip on;  
gzip\_min\_length 1100;  
gzip\_buffers 4 8k;  
gzip\_types text/plain;

sendfile on;  
tcp\_nopush on;  
tcp\_nodelay on;

keepalive\_timeout 75 20;  
proxy\_read\_timeout 180s;  
ignore\_invalid\_headers on;

index index.html;

# buffering proxy off (fake speed improvement)

proxy\_buffering off;  
proxy\_buffer\_size 128k;  
proxy\_buffers 4 256k;

# definition of the load balancing nodes

upstream backend {  
ip\_hash;  
server SYS\_SERVER1:80;  
server SYS\_SERVER2:80;  
}

# set a general temp path

proxy\_temp\_path /tmp/cache/tmp;

# include all vhosts

include sites-enabled/\*;  
}

Default vhost without caching that catches all non-specific requests:

# standard load balancer

server {  
listen SYSserver:80;  
server\_name \_;

```
            # status
            location /nginx_status {
                    stub_status on;
                    access_log off;
                    allow all;
                    #deny all;
            }

location / {
  proxy_pass http://backend;
  proxy_buffering off;
  proxy_redirect off;
  proxy_set_header Host

```

$host;  
proxy\_set\_header X-Real-IP  
$remote\_addr;  
proxy\_set\_header X-Forwarded-For  
$proxy\_add\_x\_forwarded\_for;  
}  
}

A Vhost that has caching enabled and working:

# definition about the cache

proxy\_cache\_path /tmp/cache/siteA levels=1:2  
keys\_zone=siteA:10m max\_size=1g inactive=1h;

# listener

server {  
listen SYSserver:80;  
server\_name [www.sitea.com](http://www.sitea.com);

```
location / {
  proxy_pass http://backend;

  proxy_buffering on;
  proxy_cache siteA;
                    proxy_cache_valid 200 10m;
                    proxy_cache_use_stale error timeout

```

invalid\_header updating http\_500 http\_502 http\_503 http\_504;  
proxy\_cache\_key  
“$scheme$host$request\_uri$cookie\_user”;

```
  proxy_redirect off;
  proxy_set_header Host

```

$host;  
proxy\_set\_header X-Real-IP  
$remote\_addr;  
proxy\_set\_header X-Forwarded-For  
$proxy\_add\_x\_forwarded\_for;  
}  
}

If we add the following vhost to be cached too, the whole system gets  
cached  
which shouldn’t be:

# definition about the cache

proxy\_cache\_path /tmp/cache/felix levels=1:2  
keys\_zone=felix:10m max\_size=1g inactive=1h;

# felix listener

server {  
listen CONserver:80;  
server\_name [www.siteb.com](http://www.siteb.com);

```
location / {
  proxy_pass http://backend;

  proxy_buffering on;
  proxy_cache felix;
                    proxy_cache_valid 200 10m;
                    proxy_cache_use_stale error timeout

```

invalid\_header updating http\_500 http\_502 http\_503 http\_504;  
proxy\_cache\_key  
“$scheme$host$request\_uri$cookie\_user”;

```
  proxy_redirect off;
  proxy_set_header Host

```

$host;  
proxy\_set\_header X-Real-IP  
$remote\_addr;  
proxy\_set\_header X-Forwarded-For  
$proxy\_add\_x\_forwarded\_for;  
}  
}

If you see anything that is wrong or could be configured better, please  
let  
me know. This weird caching faulty behaviour is confusing me since nginx  
won’t tell me any error.

Thanks in advance! Juergen

---

<div class="post-metadata">

**Author:** ![DiplSIngSJuergen\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@DiplSIngSJuergen\_S](https://www.ruby-forum.com/u/DiplSIngSJuergen_S)\
**Post date:** [February 21, 2012, 3:38pm UTC](https://www.ruby-forum.com/t/weird-caching-behaviour/217097/2 "2012-02-21T15:38:22Z")

</div>

Hello!

On Tue, Feb 21, 2012 at 09:27:51AM -0500, Dipl.-Ing. Juergen Ladstaetter  
wrote:

> Hi guys,
> 
> we’re running a load balanced cluster with nginx as load balancing software  
> and use the caching feature. So far we’re caching for 3 high frequent sites  
> and it’s working great.  
> Now when I add another site to be cached (configuration is below) nginx  
> starts to cache EVERY website that it’s loadbalancing. Can you find an error  
> in the configuration or tell me why it’s doing this? Thanks in advance.
> 
> Nginx.conf:

[…]

> # include all vhosts
> 
> include sites-enabled/\*;  
> }
> 
> Default vhost without caching that catches all non-specific requests:
> 
> # standard load balancer
> 
> server {  
> listen SYSserver:80;  
> server\_name \_;

This is _not_ default vhost, as you don’t have “default\_server”  
option in listen directive, and order isn’t guaranteed due to  
“include sites-enabled/\*” used.

Using

```
listen SYSserver:80 default_server;

```

should fix your problem.

More details may be found here:

> **[Server names](http://nginx.org/en/docs/http/server_names.html)**

Maxim D.

---

<div class="post-metadata">

**Author:** ![DiplSIngSJuergen\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@DiplSIngSJuergen\_S](https://www.ruby-forum.com/u/DiplSIngSJuergen_S)\
**Post date:** [February 21, 2012, 4:00pm UTC](https://www.ruby-forum.com/t/weird-caching-behaviour/217097/3 "2012-02-21T16:00:08Z")

</div>

That’s it. Thanks very much

-----Ursprngliche Nachricht-----  
Von: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Im Auftrag  
von  
Maxim D.  
Gesendet: Tuesday, February 21, 2012 9:38 AM  
An: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Betreff: Re: weird caching behaviour

Hello!

On Tue, Feb 21, 2012 at 09:27:51AM -0500, Dipl.-Ing. Juergen Ladstaetter  
wrote:

> Hi guys,
> 
> we’re running a load balanced cluster with nginx as load balancing  
> software and use the caching feature. So far we’re caching for 3 high  
> frequent sites and it’s working great.  
> Now when I add another site to be cached (configuration is below)  
> nginx starts to cache EVERY website that it’s loadbalancing. Can you  
> find an error in the configuration or tell me why it’s doing this? Thanks  
> in advance.
> 
> Nginx.conf:

[…]

> # include all vhosts
> 
> include sites-enabled/\*;  
> }
> 
> Default vhost without caching that catches all non-specific requests:
> 
> # standard load balancer
> 
> server {  
> listen SYSserver:80;  
> server\_name \_;

This is _not_ default vhost, as you don’t have “default\_server”  
option in listen directive, and order isn’t guaranteed due to “include  
sites-enabled/\*” used.

Using

```
listen SYSserver:80 default_server;

```

should fix your problem.

More details may be found here:

> **[Server names](http://nginx.org/en/docs/http/server_names.html)**

Maxim D.

* * *

nginx mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[mailman.nginx.org archive](http://mailman.nginx.org/pipermail/)**
