B
B
BoriHagen2020-04-09 20:19:36
Nginx
BoriHagen, 2020-04-09 20:19:36

How to configure Nginx + Apache 2 caching?

There is a website hosted. You need to bring it to the top in Google Page Speed ​​(no need to spit, that's what the client wants). The site runs on Nginx + Apache 2. I tried to insert the following structure into .htaccess :

<FilesMatch ".(gif|jpg|jpeg|png|ico|js|css|ttf|woff|woff2)$">
  Header set Cache-Control "max-age=604800"
</FilesMatch>

Zero result.

Then I inserted the following construction into the nginx.conf config in http {}:

server {
    location ~* ^.+\.(jpg|jpeg|gif|png|ico|js|css|ttf|woff|woff2)$ {
        expires 1w;
        add_header Cache-Control private;

    }
}

Restarted nginx. Everything went without errors. Tested for validity service nginx status - says everything is ok.

But caching never happens. I don’t know what to do, I have already rummaged through a bunch of articles, and tried a bunch of options.
There is an idea that I am not referring to the location correctly, but I don’t understand what exactly is wrong ...

Also, the image headers:

tRXyl.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mrsaa, 2020-04-10
@BoriHagen

in /etc/nginx/nginx.conf paste:

proxy_cache_path   /tmp/nginx_static levels=1:2 keys_zone=static_cache:30m max_size=100m inactive=60m use_temp_path=off;

in your config, in the location where you proxy requests for Apache, insert:
proxy_cache            static_cache;
proxy_cache_revalidate on;
proxy_cache_lock       on;
proxy_cache_valid      200 304 60m;
add_header             X-Cache-Status $upstream_cache_status;
add_header             Cache-Control "private";
add_header             Cache-Control max-age=3600;

You can leave these options or edit them to suit your needs.

B
Berkutman, 2020-04-09
@Berkutman

An example of caching, if you don't want to cache anything then proxy_cache_bypass $cookie_nocache;
proxy_cache_path /etc/nginx/cache keys_zone=cache:30m;
upstream backend {
server 127.0.0.1:8088 max_fails=4 fail_timeout=43s;
}
location / {
proxy_pass http://backend;
proxy_cache cache;
proxy_cache_valid 40s;
}
location / {
proxy_pass http://backend;
proxy_cache cache;
proxy_cache_valid 40s;
}
location ~* \.php$ {
proxy_cache_bypass $cookie_nocache;
proxy_pass http://backend;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question