Nginx配置不记录静态文件、过期时间
  2OOtxGRti36O 2023年11月13日 25 0

用户访问web网站,通常日志文件会记录很多web站点上的一些静态文件信息,如果长期不处理,日志文件会越来越大,占用的系统资源也越大,此时就需要我们配置不记录静态文件和过期时间,减少日志文件记录过多不必要的内容信息和系统资源占用。

1配置

1.1编写conf文件

[root@host ~]# vim /usr/local/nginx/conf/vhosts/test.conf

说明:expires定义的是过期时间,

加入下面内容

 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

    access_log 0ff;

    expires 15d; 

}

    location ~ (js|css)

{

    access_log off;

    expires 15h; 

}

1.2重新加载Nginx

[root@host ~]# nginx -s reload

2测试 

2.1创建两个测试文件,分别后缀为js、jpg

[root@host ~]# echo "test js" > /data/www/1.js

[root@host ~]# echo "test jpg" > /data/www/2.jpg

2.2用curl访问测试

[root@host ~]# curl -x127.0.0.1:80 www.test.com/1.js

test js

[root@host ~]# curl -x127.0.0.1:80 www.test.com/2.jpg

test jpg

[root@host ~]# curl -x127.0.0.1:80 www.test.com/index.htm

<html>

<head><title>404 Not Found</title></head>

<body bgcolor="white">

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.14.2</center>

</body>

</html>

3查看日志是否记录

[root@host ~]# cat /tmp/access.log

127.0.0.1 - - [08/Oct/2019:05:54:10 +0800] "GET HTTP://www.test.com/index.htm HTTP/1.1" 404 169 "-" "curl/7.29.0"

上面可以看出只记录了一条index.htm的URL访问记录

4验证是否显示过期时间

[root@host ~]# curl -x127.0.0.1:80 www.test.com/2.jpg -I

说明:max-age=604800显示的是过期时间,如果没有设置则这行不显示。

HTTP/1.1 200 OK

Server: nginx/1.14.2

Date: Mon, 07 Oct 2019 21:56:05 GMT

Content-Type: image/jpeg

Content-Length: 9

Last-Modified: Mon, 07 Oct 2019 21:48:32 GMT

Connection: keep-alive

ETag: "5d9bb2b0-9"

Expires: Mon, 14 Oct 2019 21:56:05 GMT

Cache-Control: max-age=604800

Accept-Ranges: bytes

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: Ansible 基础 下一篇: 使用Linux路由功能
  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  jnZtF7Co41Wg   2023年12月11日   29   0   0 nginx客户端服务端
  stLBpDewCLT1   2023年12月08日   28   0   0 nginx
  jnZtF7Co41Wg   2023年12月10日   22   0   0 nginx客户端服务端NFS
  eHipUjOuzYYH   2023年12月06日   26   0   0 nginxHTTP
  eHipUjOuzYYH   2023年12月06日   22   0   0 nginx加载IPV6