nginx配置静态文件过期时间
Posted Oops!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } |
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配置静态文件过期时间的主要内容,如果未能解决你的问题,请参考以下文章
nginx访问日志配置+日志切割+不记录静态文件日志+设置静态文件过期时间
Nginx访问日志日志切割及静态文件不记录日志和过期时间的配置