Weird caching behaviour

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;

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;

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

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:

http://nginx.org/en/docs/http/server_names.html

Maxim D.

That’s it. Thanks very much

-----Ursprngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] Im Auftrag
von
Maxim D.
Gesendet: Tuesday, February 21, 2012 9:38 AM
An: [email protected]
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:

http://nginx.org/en/docs/http/server_names.html

Maxim D.


nginx mailing list
[email protected]
http://mailman.nginx.org/mailman/listinfo/nginx

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