T
T
time_is_always_against_us2017-11-15 00:44:19
Nginx
time_is_always_against_us, 2017-11-15 00:44:19

Why is caching not working in NginX?

Server on CentOS. The site was fully configured through ISPmanager. From myself, I just added Alias ​​to the standard host settings (the site code is constantly reloaded, and pictures are rarely):
Alias ​​/img "/var/www/www-root/data/img" #image folder
Alias ​​/pma "/var/www/ www-root/data/pma" #phpmyadmin
The problem with caching appeared initially. Although the corresponding setting in the domain configuration in ISPmanage was set. It looked like this: every time you start the browser and open the site, it starts loading statics in a new way. If you close the deposit and run the site again in a new tab, the cache was saved. At first I attributed it to slow work due to https. Because ISPmanager can't fail on default settings...
Yesterday, NginX added the following to the standard settings: "add_header Cache-Control "max-age=2592000, public".
And lo and behold, the cache worked. Alas, not for long ... (There is a dummy site on the server - a priority site so that by IP to base.I accidentally copy-paste'ul the base nginx settings.There was a duplicate and nginx wouldn't start.When I found and fixed the problem, despite the "add_header Cache-Control "max-age=2592000, public" in the base settings site, the cache did not want to work there.)
It feels like some trifle does not work because of which the specified settings, apparently, are not used at all. That's why it's hard to google an example of settings and experiment with them...
IPSManager
5a0b60e7a2eb8809269697.jpeg
settings: Host settings:

<VirtualHost 127.0.0.1:8080>
  ServerName domain.ru
  DocumentRoot /var/www/www-root/data/www/domain.ru
  ServerAdmin [email protected]
  AddDefaultCharset off
  SuexecUserGroup www-root www-root
  CustomLog /var/www/httpd-logs/domain.ru.access.log combined
  ErrorLog /var/www/httpd-logs/domain.ru.error.log
  <FilesMatch "\.ph(p[3-5]?|tml)$">
    SetHandler application/x-httpd-php
  </FilesMatch>
  <FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
  </FilesMatch>
  <IfModule php5_module>
    php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -f [email protected]"
    php_admin_value upload_tmp_dir "/var/www/www-root/data/mod-tmp"
    php_admin_value session.save_path "/var/www/www-root/data/mod-tmp"
    php_admin_value open_basedir "/var/www/www-root/data:."
  </IfModule>
  <IfModule php7_module>
    php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -f [email protected]"
    php_admin_value upload_tmp_dir "/var/www/www-root/data/mod-tmp"
    php_admin_value session.save_path "/var/www/www-root/data/mod-tmp"
    php_admin_value open_basedir "/var/www/www-root/data:."
  </IfModule>
  SetEnvIf X-Forwarded-Proto https HTTPS=on
  Alias /img "/var/www/www-root/data/img"
    Alias /pma "/var/www/www-root/data/pma"
  ServerAlias www.domain.ru
  DirectoryIndex index.html index.php
</VirtualHost>
<Directory /var/www/www-root/data/www/domain.ru>
  Options -ExecCGI
  <IfModule php5_module>
    php_admin_flag engine on
  </IfModule>
  <IfModule php7_module>
    php_admin_flag engine on
  </IfModule>
</Directory>
<Directory /var/www/www-root/data/pma>
  php_admin_flag engine on
</Directory>

nginx settings:
server {
  server_name domain.ru www.domain.ru;
  charset off;
  index index.html index.php;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/domain.ru/*.conf;
  access_log /var/www/httpd-logs/domain.ru.access.log;
  error_log /var/www/httpd-logs/domain.ru.error.log notice;
  set $root_path /var/www/www-root/data/www/domain.ru;
  root $root_path;
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @fallback;
    }
    location / {
      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;
      expires 30d;
      add_header Cache-Control "max-age=2592000,  public";
    }
  }
  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;
  }
  return 301 https://$host:443$request_uri;
  listen SEVER_IP:80;
  listen [2a0***::2]:80;
  gzip on;
  gzip_comp_level 5;
  gzip_disable "msie6";
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}
server {
  server_name domain.ru www.domain.ru;
  ssl on;
  ssl_certificate "/var/www/httpd-cert/www-root/domain.ru.crtca";
  ssl_certificate_key "/var/www/httpd-cert/www-root/domain.ru.key";
  ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:RSA+3DES:!NULL:!RC4:!RSA+3DES;
  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  add_header Strict-Transport-Security "max-age=31536000;";
  ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
  charset off;
  index index.html index.php;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/domain.ru/*.conf;
  access_log /var/www/httpd-logs/domain.ru.access.log;
  error_log /var/www/httpd-logs/domain.ru.error.log notice;
  set $root_path /var/www/www-root/data/www/domain.ru;
  root $root_path;
  listen SEVER_IP:443;
  listen [2a0***::2]:443;
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @fallback;
    }
    location / {
      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;
      expires 30d;
      add_header Cache-Control "max-age=2592000,  public";
    }
  }
  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;
  }
  gzip on;
  gzip_comp_level 5;
  gzip_disable "msie6";
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}

PS This is not the first question on this topic here. Digging bit by bit ... When there is a solution, I will describe it in my other questions. For future generations, so to speak ...
Thank you in advance, at least for reading :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arman, 2017-11-15
@Arik

Is ISPmanager definitely not rewriting the nginx config after your edits? Those. you added who climbed what enabled and ISPmanager saves its config and reload nginx, losing your edits

S
Sanes, 2017-11-15
@Sanes

If you check through Google Pagespeed, put unlimited caching time as the last item.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question