Use browser caching for Nginx, without css when reloading the page

I try to follow google pagespeed tips and use browser caching. To do this, I put the following code in the server block of my nginx.conf file.

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d; } location ~* \.(pdf)$ { expires 30d; } 

It seems to work well, page speed increases my score to 87/100 to 95/100. However, when I click the update button for my site, it no longer downloads css files? Does caching not work?

The error message I get is

 Failed to load resource: the server responded with a status of 404 (Not Found) 

Here is my whole nginx.conf file

 worker_processes 1; events { worker_connections 1024; } http { include /etc/nginx/mime.types; sendfile on; gzip on; gzip_http_version 1.0; gzip_proxied any; gzip_min_length 500; gzip_disable "MSIE [1-6]\."; gzip_types text/plain text/xml text/css text/comma-separated-values text/javascript application/x-javascript application/atom+xml; # Configuration containing list of application servers upstream app_servers { server 127.0.0.1:8080; } # Configuration for Nginx server { # Running port listen 80; # Settings to serve static files location /static/ { # Example: # root /full/path/to/application/static/file/dir; root /var/www/benty-fields/app/; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d; } location ~* \.(pdf)$ { expires 30d; } # Serve a static file (ex. favico) # outside /static directory location = /favico.ico { root /app/favico.ico; } # Proxy connections to the application servers # app_servers location / { proxy_pass http://app_servers; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; } } } 
+5
source share
2 answers

Look at Fiddler traces or Chrome dev tools.

A 304 means the server replied "not changed, use your local cache." If you clear the browser cache or do Shift + Refresh, you will get 200 along with the body of the file. 304, on the contrary, have zero body length.

0
source

I had the same problem.

Allowed him by setting:

 location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d; } location ~* \.(pdf)$ { expires 30d; } 

inside

location / static /

So the final configuration looks like

 location / { proxy_pass http://app_servers; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d; } location ~* \.(pdf)$ { expires 30d; } } 

Link: https://developers.google.com/speed/pagespeed/module/filter-cache-extend

0
source

Source: https://habr.com/ru/post/1233456/


All Articles