NGINX - Reverse Proxy With Caching
Example
http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g; server { location / { proxy_pass http://1.2.3.4; proxy_set_header Host $host; proxy_buffering on; proxy_cache STATIC; proxy_cache_valid 200 1d; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } }
proxy_cache_path
Syntax:
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Sets the path and other parameters of a cache. Cache data are stored in files. The file name in a cache is a result of applying the MD5 function to the cache key. The levels parameter defines hierarchy levels of a cache: from 1 to 3, each level accepts values 1 or 2. For example, in the following configuration .
proxy_pass
Syntax:
proxy_pass URL;
Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a domain name or IP address, and an optional port:
proxy_pass http://localhost:8000/uri/;
or as a UNIX-domain socket path specified after the word “unix” and enclosed in colons:
proxy_pass http://unix:/tmp/backend.socket:/uri/;
proxy_set_header
Syntax:
proxy_set_header field value; Default: proxy_set_header Host $proxy_host; proxy_set_header Connection close;
proxy_buffering
Syntax:
proxy_buffering on | off; Default: proxy_buffering on;
proxy_cache
Syntax:
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...; Default: proxy_cache_use_stale off;
Determines in which cases a stale cached response can be used during communication with the proxied server.
proxy_cache_valid
Syntax:
roxy_cache_valid [code ...] time;
Sets caching time for different response codes. For example, the following directives
proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
set 10 minutes of caching for responses with codes 200 and 302 and 1 minute for responses with code 404.
If only caching time is specified
proxy_cache_valid 5m;
then only 200, 301, and 302 responses are cached.
In addition, the any parameter can be specified to cache any responses:
proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m;
proxy_cache_use_stale
Syntax:
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...; Default: proxy_cache_use_stale off;
Determines in which cases a stale cached response can be used during communication with the proxied server.
Sources
- https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/
- https://nginx.org/en/docs/http/ngx_http_proxy_module.html