Nginx caching reverse proxy configuration

I was pretty impressed by how easy it was to get Nginx to work as a caching reverse proxy. I run a slightly odd configuration where my host throws out ssh tunnels to an EC2 instance, this then feeds into Cloudflare for CDN.

I decided to put Nginx in the middle. While Cloudflare is good, it doesn’t really do much by way of aggressive caching and the SSH tunnels were still getting hit pretty hard. In the free Cloudflare plan you don’t get much control over this anyway.

So I switched up my tunnels and installed Nginx on the EC2 instance. Configured this as a reverse proxy and set reasonably aggressive caching. Config follows:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

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

	##
	# Logging Settings
	##

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

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";
	proxy_cache_path /data/nginx/cache keys_zone=one:10m;

	server {
		listen 80;
		proxy_cache one;
		proxy_cache_min_uses 100;
		proxy_cache_valid 200 302 30m;
		proxy_cache_valid 404      1m;
		    proxy_set_header Host $host;
		location / {
            		proxy_pass http://localhost:8090;
        	}
    	}
}

Comments are closed.