12.12 静态文件不记录日志和过期时间

时间:2022-04-27
本文章向大家介绍12.12 静态文件不记录日志和过期时间,主要内容包括静态文件不记录日志和过期时间目录概要、静态文件不记录日志和过期时间、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

静态文件不记录日志和过期时间目录概要

  • 配置如下
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) ”关闭记录日志
    }
  1. 打开虚拟主机配置文件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;
}   
保存退出
  1. 检查配置文件语法错误,并重新加载配置文件
[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]# 
  1. 测试,先来模拟一个图片
[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]# 
  1. 接下来做一个访问测试
[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]# 
  1. 查看日志,会看到只有一条日志
[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]# 
  1. 测试过期时间,加上-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]# 
  • max-age=43200 过期时间
  1. 如果去掉expires,则不会显示max-age过期时间