nginx配置静态文件过期时间

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;
    }

   wKioL1iuqD6CrZIPAAAYAw69t2k549.png

 

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

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

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

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

 wKiom1iuqE7j4aIOAADopq91EZA697.png

 

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

 wKioL1iuqFiyDEfwAAA7br8VW5g187.png

 

 

实际配置:

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

 

 
posted @ 2018-08-30 15:09  Oops!#  阅读(3682)  评论(0编辑  收藏  举报