A
A
Azami2019-01-01 10:52:46
Nginx
Azami, 2019-01-01 10:52:46

How to properly cache nginx static?

Hello! Happy New Year. I have a site.ru.conf file containing:

server {
  server_name site.ru *.site.ru www.site.ru;
  charset off;
  index index.php index.html;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/site.ru/*.conf;
  access_log /var/www/httpd-logs/site.ru.access.log;
  error_log /var/www/httpd-logs/site.ru.error.log notice;
  ssi on;
  set $root_path /var/www/user-admin/data/www;
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @fallback;
    }
    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
      try_files $uri $uri/ @fallback;
    }
    location / {
      try_files /does_not_exists @fallback;
    }
  }
  location @fallback {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect http://127.0.0.1:8080 /;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
    access_log off;
  }
    set $subdomain site.ru;
  if ($host ~* ^((.*).site.ru)$) {
    set $subdomain $1;
  }
  root $root_path/$subdomain;
  return 301 https://$host:443$request_uri;
  listen 99.999.99.999:80;
}
server {
  server_name site.ru *.site.ru www.site.ru;
  ssl_certificate "/var/www/httpd-cert/user-admin/site.ru_le1.crtca";
  ssl_certificate_key "/var/www/httpd-cert/user-admin/site.ru_le1.key";
  ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
  charset off;
  index index.php index.html;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/site.ru/*.conf;
  access_log /var/www/httpd-logs/site.ru.access.log;
  error_log /var/www/httpd-logs/site.ru.error.log notice;
  ssi on;
  set $root_path /var/www/user-admin/data/www;
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @fallback;
    }
    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
      try_files $uri $uri/ @fallback;
    }
    location / {
      try_files /does_not_exists @fallback;
    }
  }
  location @fallback {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect http://127.0.0.1:8080 /;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
    access_log off;
  }
    set $subdomain site.ru;
  if ($host ~* ^((.*).site.ru)$) {
    set $subdomain $1;
  }
  root $root_path/$subdomain;
  listen 99.999.99.999:443 ssl;
}

What can I do to make caching work?
I paste the code
location ~* ^.+\.(jpg|jpeg|gif|png|txt|js|css|ico)$ {
      expires 1d;
      add_header Cache-Control private;
    }

I restart nginx, but to no avail, tell me what I'm doing wrong or not completely.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Syomov, 2019-01-01
@kotomyava

You already have a location where these files are processed - this one:

location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
      try_files $uri $uri/ @fallback;
    }

It is also necessary to insert expires into it. And it has an extra argument in try_files - why is $uri/ there if you process only files? This is more correct: try_files $uri @fallback;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question