sunnuntai 25. helmikuuta 2018

Using NGINX as web cache

Sometimes you have to work with an HTTP(S) service that's too slow for your purposes. For example, you might have to test your service against HTTP based interface, which takes ages to respond and you don't want to spend too much testing time on waiting responses to see how your latest code change behaves. If it's ok to use cached data, you can use NGINX as web cache and proxy all your requests through it. If you have already used the same request before (most likely test code generates the same request all over again), NGINX doesn't call the real HTTP interface. Instead, it's returning a cached response you got previously.

Here's a working configuration for setting up NGINX for caching site https://www.uta.fi.

events {
  worker_connections  4096;  ## Default: 1024
}
 
http {
  proxy_cache_path /tmp/nginx-proxy levels=1:2 keys_zone=my_cache:10m max_size=10g
                 inactive=1y use_temp_path=off;
  proxy_hide_header       Set-Cookie;
  proxy_ignore_headers    Set-Cookie;
  server {
      listen   80;
      location / {
          proxy_send_timeout   180s;
          proxy_read_timeout   180s;
 
          proxy_cache my_cache;
          proxy_cache_valid 200 1y;
          proxy_pass https://www.uta.fi;
      }
  }
}

This configuration stores up to 10GB of cached data for 1 year. The local cache path is set to existing directory /tmp/nginx-proxy.

To use NGINX, you don't have to install it locally. I prefer using Docker images. You can run the example above with the following command.

docker run -d --rm -p 1001:80 --name nginx-cache \
-v /tmp/proxy-blog/nginx-proxy.conf:/etc/nginx/nginx.conf:ro \
-v /tmp/proxy-blog/proxy-local-directory:/tmp/nginx-proxy nginx

After the command, point your browser at http://localhost:1001 and see the results. The command assumes that you have saved the NGINX configuration listed above as /tmp/proxy-blog/nginx-proxy.conf and that you have directory /tmp/proxy-blog/proxy-local-directory on your computer (the Docker host).