首页 > 其他 > 详细

nginx配置静态文件过期时间

时间:2018-08-30 15:38:17      阅读:158      评论:0      收藏:0      [点我收藏+]

1. 编辑虚拟主机配置文件/usr/local/nginx/conf/vhosts/huangzhenping.conf

    说明:采用location方式

1
2
3
4
5
6
7
8
9
10
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    access_log off;
    expires 1d;
    }
    location ~ \.(js|css)
    {
       access_log off;
       expires 1d;
    }

   技术分享图片

 

2. 检查配置文件,重新加载,测试

    说明:google浏览器可以按F12,查看开发者工具或者用curl测试

    /usr/local/nginx/sbin/nginx -t

    /usr/local/nginx/sbin/nginx -s reload

 技术分享图片

 

    curl -x127.0.0.1:80 http://www.huanglearn.cn/static/image/smiley/default/mad.gif -I

 技术分享图片

 

 

实际配置:

server {
        listen       80;
        server_name  www.weifeng.cn;


        location /api/ {
            proxy_pass http://127.0.0.1:8088/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location / {
            try_files $uri /index.html;
            root   /home/*****ui/build;
            index  index.html index.htm;
        }
    
       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     {
        access_log off;
        expires 10d;
        root   /home******/ui/build;
     }

       location ~ \.(js|css)
    {
       access_log off;
       expires 10d;
       root   /home******/ui/build;
    }
        

}

测试效果:

 

 curl -x127.0.0.1:80 http://www.weofemg.cn/static/js/main.5770cbc7.js -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 30 Aug 2018 06:55:51 GMT
Content-Type: application/javascript
Content-Length: 412411
Last-Modified: Wed, 29 Aug 2018 12:28:50 GMT
Connection: keep-alive
ETag: "5b869182-64afb"
Expires: Sun, 09 Sep 2018 06:55:51 GMT
Cache-Control: max-age=864000
Accept-Ranges: bytes

 

 

nginx配置静态文件过期时间

原文:https://www.cnblogs.com/weifeng1463/p/9560311.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!