I have nginx running as a reverse proxy. Currently this is only providing centralized SSL termination and internet connectivity. I want to enable caching for static content.
The config for a virtual host on the proxy looks something like....
server {
listen 443 ssl http2;
server_name www.example.com;
...
location / {
include snippets/proxydefaults.conf;
proxy_pass http://www.example.com.origin/;
}
}
I am not confident that the caching on the origin servers is correctly setup, hence I only want to cache things which I know are static files. If I nest the caching config, do I need to repeat the config to apply the proxying, or is this inherited from the parent location....
server {
listen 443 ssl http2;
server_name www.example.com;
...
location / {
include snippets/proxydefaults.conf;
proxy_pass http://www.example.com.origin/;
location ~* \.(gif|png|jpg|jpeg|js|css)$ {
proxy_cache default_cache;
# do I need to explicitly add another proxy_pass directive here?
}
}
}
Request handlers are always explicitly set for a location. So proxy_pass won't be inherited.
Nested locations are going bite you sooner or later; the non-trivial inheritance rules and having to copy-paste configs are both very likely causes of unnecessary problems.
Instead, you can define a variable (
set $dont_cache_this 1
), set it selectively to zero on "known static" content, and use it in conjunction with the proxy_no_cache directive.