Nginx as front end cache for WMS

I tried to set up nginx as a front end cache for my WMS server. The
typical
WMS request is:
http://localhost:8080/geoserver/somestring/wms?version=1.1.1&service=WMS&request=WMS&
Below is my nginx configuration:

/etc/nginx/nginx.conf:
user vriuser;
worker_processes 4;
pid /var/run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

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

    proxy_buffering           on;
    proxy_cache_valid         any 10m;
    proxy_cache_path          /var/geoserver/cache  levels=1:2

keys_zone=one:10m inactive=7d max_size=5000m;
proxy_temp_path /var/geoserver/cache/tmp;

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}

/etc/nginx/sites-enabled/geoserver.conf
server {
listen 80;
#server_name localhost;
root /home/geoserver/webapps/geoserver;

    location /geoserver {
        proxy_pass   http://localhost:8080;
        proxy_cache             one;
        proxy_cache_key         backend$request_uri;
        proxy_cache_valid       200  5h;
        proxy_cache_use_stale   error timeout invalid_header;
    }

}

Both nginx and my WMS servers were running without errror. However,
when I
inspect the /var/geoserver/cache directory, I did not see anything file
in
there. Does it mean my configuration is not correct? How do I know if
nginx caches my WMS responses? Any help on this is greatly appreciated.

Many thanks in advance.

Regards,
Tam

Posted at Nginx Forum:

On 03/04/14 02:26, tt5430 wrote:

How do I know if
nginx caches my WMS responses?

It will be useful to log $upstream_cache_status and
$upstream_response_time in
access_log.

Also check, that geoserver return code is 200 (and not redirect to some
other URI).

Hi Anton,

I followed your suggestion and modified my conf files. I also added
some
new
stuff but still could not get it to work. I did not see any message
written
to the access log and did not see anything written to the cache. I
included
my new conf files and a request/response block that I captured using
FireFox
Live Http Headers addon. I hope someone can catch my mistake and show
me
what was wrong.

[/etc/nginx/nginx.conf]
user vriuser;
worker_processes 4;
pid /var/run/nginx.pid;

events {
worker_connections 768;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format geoserver-cache '$remote_addr - 

$upstream_cache_status
[$time_local] ’
'“$request” $status $body_bytes_sent ’
‘“$http_referer” “$http_user_agent”’;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}

[/etc/nginx/sites-enabled/proxy.conf]
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Cache $upstream_cache_status;

proxy_connect_timeout 60;
proxy_read_timeout 90;
proxy_send_timeout 60;

proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 100 8k;

[/etc/nginx/sites-enabled/geoserver.conf]
upstream backend {
server localhost;
}

proxy_cache_path /var/geoserver/cache levels=1:2
keys_zone=geoserver-cache:10m inactive=7d max_size=5000m;
proxy_temp_path /var/geoserver/cache/tmp;

server {
listen 80;
#server_name localhost;
#root /home/geoserver/webapps/geoserver;
root /opt/tomcat/webapps/geoserver;

    proxy_redirect off;
    proxy_buffering           on;
    proxy_cache_valid         any 7d;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    access_log /var/log/nginx/geoserver/access.log geoserver-cache;
    error_log /var/log/nginx/geoserver/error.log;

    proxy_cache geoserver-cache;
    proxy_cache_valid 200 2h;
    proxy_cache_valid 302 2h;
    proxy_cache_valid 301 4h;
    proxy_cache_valid any 1m;
    resolver 127.0.0.1;

    location / {
        proxy_pass http://backend;
        proxy_cache_key $scheme://backend:8080/$request_uri;
        proxy_cache_valid       200;
    }

}

Request/Response headers:

http://localhost:8080/geoserver/osm/wms?LAYERS=Florida&STYLES=&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG%3A3857&BBOX=-9046077.9720712,3223712.9634365,-9032807.336146,3240204.627499&WIDTH=412&HEIGHT=512

GET
/geoserver/osm/wms?LAYERS=Florida&STYLES=&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG%3A3857&BBOX=-9046077.9720712,3223712.9634365,-9032807.336146,3240204.627499&WIDTH=412&HEIGHT=512
HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)
Gecko/20100101
Firefox/23.0
Accept: image/png,image/;q=0.8,/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer:
http://localhost:8080/geoserver/osm/wms?service=WMS&version=1.1.0&request=GetMap&layers=Florida&styles=&bbox=-9735065.0,2733130.75,-8885187.0,3788597.25&width=412&height=512&srs=EPSG:3857&format=application/openlayers
Cookie: JSESSIONID=BDE023A90406794200BCF412045BC96C;
JSESSIONID=maqv107kg0me
Connection: keep-alive

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: inline; filename=Florida.png
Content-Type: image/png
Transfer-Encoding: chunked
Date: Wed, 05 Mar 2014 00:15:35 GMT

Regards,
Tam

Posted at Nginx Forum: