Состояние кэша Nginx Отсутствует даже после добавления кеширования

Я новичок в кешировании на nginx. Я пытался установить кеширование на сервере, но X-Cache-Status выдает MISS даже после добавления конфигураций для css / js / и изображений. Это приводит к большому времени загрузки и низкой скорости даже при низком трафике. Есть идеи, что мне не хватает?

Ниже приводится заголовок ответа:

Cache-Control   
max-age=86400
Connection  
keep-alive
Content-Encoding    
gzip
Content-Type    
text/css
Date    
Wed, 18 Jan 2017 16:00:34 GMT
Expires 
Thu, 19 Jan 2017 16:00:34 GMT
Last-Modified   
Mon, 19 Oct 2015 09:26:18 GMT
Server  
nginx/1.4.6 (Ubuntu)
Transfer-Encoding   
chunked
Vary    
Accept-Encoding
X-Cache-Status  
MISS

Я использую https, где перенаправляю http на https. Вот файл с поддержкой сайтов, который я настроил.

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=TOMCAT:50m max_size=100m;
server {
   listen   80;
   server_name *.xyz.com;
   default_type text/html; 
   return 307 https://$host$request_uri;
   root   /var/lib/tomcat7/webapps;
   index  index.html index.jsp;
      location / {
    set $no_cache "";
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1";
        }
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0";
        }
        if ($http_cookie ~* "_mcnc") {
            set $no_cache "1";
        }
    if ($request_uri ~* ".(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
    expires 1d;
    access_log off;
    break;
    }
      proxy_no_cache $no_cache;
        proxy_cache_bypass $no_cache;
        proxy_cache TOMCAT;
        proxy_cache_key $scheme$host$request_method$request_uri;
        proxy_cache_valid 200 302 1s;
        proxy_cache_valid 301 1s;
        proxy_cache_valid any 1s;
        proxy_cache_use_stale updating;
      sendfile off;
      proxy_pass         http://127.0.0.1:8080/;
      proxy_redirect     default;

      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_max_temp_file_size 0;
    add_header X-Cache-Status $upstream_cache_status;
      #this is the maximum upload size
      client_max_body_size       10m;
      client_body_buffer_size    128k;

      proxy_connect_timeout      90;
      proxy_send_timeout         90;
      proxy_read_timeout         90;

      proxy_buffer_size          4k;
      proxy_buffers              4 32k;
      proxy_busy_buffers_size    64k;
      proxy_temp_file_write_size 64k;
   }
}

server {
        server_name *.xyz.com;
        listen 443;

        ssl on;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #make sure you already have this certificate pair!
    ssl_certificate /etc/ssl/certs/xyz.crt;
        ssl_certificate_key /etc/ssl/certs/xyz.key;
        ssl_session_cache shared:SSL:10m;

        location / {
            set $no_cache "";
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1"; 
        }   
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0"; 
        }   
        if ($http_cookie ~* "_mcnc") {
            set $no_cache "1";
        } 
    if ($request_uri ~* ".(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
    expires 1d;
    access_log off;
    break;
    }  
      proxy_no_cache $no_cache;
        proxy_cache_bypass $no_cache;
        proxy_cache TOMCAT;
        proxy_cache_key $scheme$host$request_method$request_uri;
        proxy_cache_valid 200 302 1s;
        proxy_cache_valid 301 1s;
        proxy_cache_valid any 1s;
        proxy_cache_use_stale updating;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto https;
    add_header X-Cache-Status $upstream_cache_status;
            proxy_redirect off;
            proxy_connect_timeout      240;
            proxy_send_timeout         240;
            proxy_read_timeout         240;
            # note, there is not SSL here! plain HTTP is used
       proxy_pass http://127.0.0.1:8081;
        }
     }

Есть предложения?

0
задан 18 January 2017 в 18:18
1 ответ

pur și simplu adăugați acest lucru. Aceasta setează data de expirare la data maximă (ceva de genul 2037 după cum îmi amintesc) și controlul cache-ului la 10 ani.

location ~* \.(css|gif|ico|jpeg|jpg|js|png|woff|woff2|ttf|ttc|otf|eot)$ {
        expires max;
        log_not_found off;
    }
0
ответ дан 5 December 2019 в 08:43

Теги

Похожие вопросы