静态文件不记录日志和过时时间目录概要
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
静态文件不记录日志和过时时间
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配gif|jpg|jpeg|png|bmp|swf 后缀的文件
{
expires 7d; //7天后过时
access_log off; //匹配“.*.(gif|jpg|jpeg|png|bmp|swf) ”关闭记录日志
}
location ~ .*\.(js|css)$
{
expires 12h; //12个小时后过时
access_log off; //匹配“.*.(js|css) ”关闭记录日志
}
- 打开虚拟主机配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
[root@hanfeng vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/test.com.log combined_realip;
}
保存退出
- 检查配置文件语法错误,并从新加载配置文件
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
- 测试,先来模拟一个图片
[root@hanfeng vhost]# cd /data/wwwroot/test.com/
[root@hanfeng test.com]# ls
admin index.html
[root@hanfeng test.com]# vim 1.gif 在1.gif随意写入一些内容
[root@hanfeng test.com]# vim 2.js
[root@hanfeng test.com]#
- 接下来作一个访问测试
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/1.gif
sdafasf
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/2.js
fghdfsd
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/index.html
“test.com”
[root@hanfeng test.com]#
- 查看日志,会看到只有一条日志
[root@hanfeng test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jan/2018:00:17:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@hanfeng test.com]#
- 测试过时时间,加上-I参数
[root@hanfeng test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 16:22:07 GMT
Content-Type: application/javascript
Content-Length: 8
Last-Modified: Thu, 04 Jan 2018 16:15:42 GMT
Connection: keep-alive
ETag: "5a4e532e-8"
Expires: Fri, 05 Jan 2018 04:22:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
[root@hanfeng test.com]#
- 若是去掉expires,则不会显示max-age过时时间