前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >12.12 静态文件不记录日志和过期时间

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

作者头像
运维小白
发布2018-02-06 15:54:28
发布2018-02-06 15:54:28
1.2K00
代码可运行
举报
文章被收录于专栏:运维小白运维小白
运行总次数:0
代码可运行

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

  • 配置如下
代码语言:javascript
代码运行次数:0
运行
复制
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }

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

  • 在配置文件中添加
代码语言:javascript
代码运行次数:0
运行
复制
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
代码语言:javascript
代码运行次数:0
运行
复制
[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. 检查配置文件语法错误,并重新加载配置文件
代码语言:javascript
代码运行次数:0
运行
复制
[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. 测试,先来模拟一个图片
代码语言:javascript
代码运行次数:0
运行
复制
[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. 接下来做一个访问测试
代码语言:javascript
代码运行次数:0
运行
复制
[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. 查看日志,会看到只有一条日志
代码语言:javascript
代码运行次数:0
运行
复制
[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参数
代码语言:javascript
代码运行次数:0
运行
复制
[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过期时间
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 静态文件不记录日志和过期时间目录概要
  • 静态文件不记录日志和过期时间
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档