为图像,css,js和字体添加Cache-Control到Nginx

前端之家收集整理的这篇文章主要介绍了为图像,css,js和字体添加Cache-Control到Nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试让Cache-Control在Nginx上运行我的服务器上的资产并且没有按预期运行.这是我的Nginx服务器配置.

除了Cache-Control之外的一切都在工作.

服务器块

server {
   listen 80;
   server_name www.example.com;
   rewrite ^ https://$server_name$request_uri? permanent;
}

server {
   listen 80;
   server_name example.com;
   rewrite ^ https://www.$server_name$request_uri? permanent;
}

server {
   listen 443 ssl;
   server_name default;
   root /app/public;

   ssl_certificate /etc/Nginx/ssl/default/crt/server.crt;
   ssl_certificate_key /etc/Nginx/ssl/default/crt/server.key;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   index index.html index.htm index.PHP;
   charset utf-8;
   add_header "X-UA-Compatible" "IE=Edge,chrome=1";

   location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)${
       expires 1d;
       access_log off;
       add_header Pragma public;
       add_header Cache-Control "public,max-age=86400";
       add_header X-Asset "yes";
   }

   location / {
      try_files $uri $uri/ /index.PHP?$query_string;
   }

   location = /favicon.ico { access_log off; log_not_found off; }
   location = /robots.txt { access_log off; log_not_found off; }
   access_log off;
   error_log /var/log/Nginx/default-error.log error;

   error_page 404 /index.PHP;
   location ~ \.PHP${
      fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
      fastcgi_pass unix:/var/run/PHP/PHP7.0-fpm.sock;
      fastcgi_index index.PHP;
      include fastcgi_params;
   }

   location ~ /\.ht {
      deny all;
   }
}

以下是我的服务器对https://www.example.com/icons/facebook.png的回复

Accept-Ranges:bytes
Cache-Control:public,max-age=120
Connection:keep-alive
Content-Length:416
Content-Type:image/png
Date:Tue,04 Oct 2016 14:46:26 GMT
ETag:"57f2a0fd-1a0"
Last-Modified:Mon,03 Oct 2016 18:18:37 GMT
Server:Nginx/1.11.2

max-age是120,我期待86400,并且没有自定义X-Asset标头.

HTTP块

/etc/Nginx/conf.d/文件夹为空.

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/Nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3,ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/Nginx/access.log;
    error_log /var/log/Nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
    # gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+RSS text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/Nginx/conf.d/*.conf;
    include /etc/Nginx/sites-enabled/*;
}

解决方法

您的来源是否发回了Cache-Control标头?或者这是直接从文件系统提供的文件之一.

如果是这样,也许你只是在更改后没有重新加载Nginx? sudo Nginx -s reload

与您的直接问题分开,但只是注意该行:

add_header "X-UA-Compatible" "IE=Edge,chrome=1";

将不适用于包含它自己的add_header指令的任何位置(您可以在粘贴的输出中看到这一点).

猜你在找的CSS相关文章