Answer the question
In order to leave comments, you need to log in
How to properly configure cache on nginx?
Colleagues, hello!
I'm trying to set up a normal cache. I have one location that checks if a file exists in a folder and (if not) redirects to a named location (the one with @). Named location makes proxy_pass to nodejs server. I would like all files that came with nodejs to be cached. But only files (js/css). Is there any adequate way to set such a limit? proxy_cache_bypass looks like what you need, but it's not clear how to set it up.
An example of how I set it up now will be in the spoiler below. With these settings, nginx does not save the cache in /var/cache/nginx at all, and when you try to load it, it swears that the file is not in /usr/share/nginx/html/.
proxy_cache_path /var/cache/nginx levels=2 keys_zone=pagecache:5m inactive=10m max_size=50m;
server {
listen 80;
server_name domain.ru;
proxy_cache_bypass 1;
location ~(\.js|\.css)$ {
proxy_cache_bypass 0;
}
location / {
root lp;
rewrite ^(/pricing|/contacts)/?(.*) /$2 break;
proxy_cache pagecache;
proxy_cache_valid 10m;
try_files $uri/index.html $uri @custom;
}
location @custom {
proxy_pass http://ru_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Answer the question
In order to leave comments, you need to log in
proxy_cache_path /var/cache/nginx levels=2 keys_zone=pagecache:5m inactive=10m max_size=50m;
server {
listen 80;
server_name domain.ru;
root /var/www/html;
rewrite ^/(pricing|contacts)/?(.*) /$2;
location ~ \.(js|css)$ {
try_files $uri @custom_cache;
}
location / {
try_files $uri/index.html $uri @custom;
}
location @custom {
proxy_pass http://ru_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location @custom_cache {
proxy_cache pagecache;
proxy_cache_valid 10m;
proxy_pass http://ru_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question