upstream dns-backend { server 127.0.0.1:8053; keepalive 30; } server { server_name dns.bentasker.co.uk; root /tmp/NOEXIST; location /dns-query { # Apply rate limiting limit_req zone=doh_limit burst=50 nodelay; # Process the IP to build a cache key set $ip_str $remote_addr; if ($ip_str ~ ([0-9,\.]*)\.([0-9]*)){ set $ip_str "$1.0/24"; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection ""; proxy_redirect off; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 86400; proxy_pass http://dns-backend/dns-query; # Include the client's /24 in the cache key proxy_cache doh-cache; proxy_cache_key https$host$request_uri|$ip_str; proxy_cache_valid 200 30s; proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; # If two identical requests are received simnultaneously, make one wait proxy_cache_lock on; } location / { return 404; } listen 443 ssl http2; # managed by Certbot ssl_certificate /etc/letsencrypt/live/dns.bentasker.co.uk/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/dns.bentasker.co.uk/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot }