Nginx does not cache static for subdomains. The pagespeed service shows that the cache is enabled, but when the file changes, the updated code is taken from the server, and not from the browser cache.

Response Headers
Cache-Control: public
Cache-Control: max-age = 2592000
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text / css
Date: Mon, 23 May 2016 12:00:31 GMT
Expires: Wed, 22 Jun 2016 12:00:31 GMT
Last-Modified: Mon, 23 May 2016 11:59:30 GMT
Server: nginx / 1.4.6 (Ubuntu)
Transfer-Encoding: chunked
Vary: Accept-Encoding

Nginx settings

server { listen *:80; listen [::]:80; server_name site.ru; root /home/site/public; index index.php index.html; access_log /var/log/nginx/site.access.log; error_log /var/log/nginx/site.error.log; client_max_body_size 10m; client_body_buffer_size 128k; location / { try_files $uri $uri/ /index.php?$query_string; } if (!-d $request_filename) { rewrite ^/(.+)/$ /$1 permanent; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 30; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; } location ~* \.(css)$ { if ($request_uri ~ ^/css(/.+)$) { set $modify_request_uri $1; return 301 $scheme://css.site.ru$modify_request_uri; } } } server { listen *:80; server_name css.site.ru; root /home/site/public/css; access_log /var/log/nginx/css.site.access.log; error_log /var/log/nginx/css.site.error.log; location ~* { add_header Cache-Control public; expires 30d; } } 

Reply to comment from norbornen

if_modified_since off and hide the Last-Modified header

Response Headers
Cache-Control: max-age = 2592000
Cache-Control: public
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text / css
Date: Mon, 23 May 2016 13:26:54 GMT
Expires: Wed, 22 Jun 2016 13:26:54 GMT
Server: nginx / 1.4.6 (Ubuntu)
Transfer-Encoding: chunked Vary: Accept-Encoding

Still not working.

  • That's right, the file on the server has been changed! - Artyomich
  • it is necessary that it be given from the browser cache until the time expires in the Expires heading - user211192
  • What HTTP specification are you using? - Artyomich
  • if_modified_since off and hide the header Last-Modified - nörbörnën
  • And how do you check this? Shake f5? - Alexey Ten

1 answer 1

It can help:

 # кеширование в браузере на стороне пользователя <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access 7 days" ExpiresByType application/javascript "access plus 1 year" ExpiresByType text/javascript "access plus 1 year" ExpiresByType text/css "access plus 1 year" ExpiresByType text/html "access plus 7 day" ExpiresByType text/x-javascript "access 1 year" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType image/jpg "access plus 1 year" ExpiresByType image/x-icon "access 1 year" ExpiresByType application/x-shockwave-flash "access 1 year" </IfModule> # Cache-Control <ifModule mod_headers.c> # 30 дней <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=2592000, public" </filesMatch> # 30 дней <filesMatch "\.(css|js)$"> Header set Cache-Control "max-age=2592000, public" </filesMatch> # 2 дня <filesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=172800, public, must-revalidate" </filesMatch> # 1 день <filesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=172800, private, must-revalidate" </filesMatch> </ifModule> #Запрет отдачи HTTP-заголовков Vary браузерам семейства MSIE <IfModule mod_setenvif.c> BrowserMatch "MSIE" force-no-vary BrowserMatch "Mozilla/4.[0-9]{2}" force-no-vary </IfModule> 

Original source

  • I use nginx + php-fpm without apache - user211192