【CentOS 7LNMP架构26】,静态文件不记录日志和过期时间#

本文详细介绍了如何使用Nginx配置静态文件的缓存时间及日志记录关闭,通过具体实例展示了配置过程及其效果,包括.gif、.jpg等图片和.js、.css文件的缓存策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

shallow丿ove


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

  • 配置如下 location ~..(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~..(js|css)$ { expires 12h; access_log off; }
[root@localhost vhost]# vi test.com.conf 
      1 server
      2 {
      3         listen 80;
      4         server_name test.com test1.com test2.com;
      5         index index.html index.htm index.php;
      6         root /data/wwwroot/test.com;
      7         if ($host != 'test.com'){
      8                 rewrite ^/(.*)$ http://test.com/$1 permanent;
      9         }
     10 
     11         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     12         {
     13                 expires 7d;
     14                 access_log off;
     15         }
     16         location ~ .*\.(js|css)$
     17         {
     18                 expires 12h;
     19                 access_log off;
     20         }
     21 
     22         access_log /tmp/test.com.log;
     23 }
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# cd /data/wwwroot/test.com/
[root@localhost test.com]# echo gif > 1.gif
[root@localhost test.com]# echo js > 2.js
[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/index.html
test.com
[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/1.gif
gif
[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/2.js
js
[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/index.html
test.com

[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - - [04/Jan/2018:16:51:53 +0800] "GET HTTP://test.com/index.html HTTP/1.1" 200 9 "-" "curl/7.29.0"
127.0.0.1 - - [04/Jan/2018:16:52:08 +0800] "GET HTTP://test.com/index.html HTTP/1.1" 200 9 "-" "curl/7.29.0"
[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/3.php
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - - [04/Jan/2018:16:51:53 +0800] "GET HTTP://test.com/index.html HTTP/1.1" 200 9 "-" "curl/7.29.0"
127.0.0.1 - - [04/Jan/2018:16:52:08 +0800] "GET HTTP://test.com/index.html HTTP/1.1" 200 9 "-" "curl/7.29.0"
127.0.0.1 - - [04/Jan/2018:16:53:57 +0800] "GET HTTP://test.com/3.php HTTP/1.1" 404 168 "-" "curl/7.29.0"

过期时间

[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 04 Jan 2018 08:54:59 GMT
Content-Type: application/x-javascript
Content-Length: 3
Last-Modified: Thu, 04 Jan 2018 08:50:53 GMT
Connection: keep-alive
ETag: "5a4deaed-3"
Expires: Thu, 04 Jan 2018 20:54:59 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

max-age=43200是由expires 12h生成

[root@localhost test.com]# vi /usr/local/nginx/conf/vhost/test.com.conf
      1 server
      2 {
      3         listen 80;
      4         server_name test.com test1.com test2.com;
      5         index index.html index.htm index.php;
      6         root /data/wwwroot/test.com;
      7         if ($host != 'test.com'){
      8                 rewrite ^/(.*)$ http://test.com/$1 permanent;
      9         }
     10 
     11         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     12         {
     13 #               expires 7d;
     14                 access_log off;
     15         }
     16         location ~ .*\.(js|css)$
     17         {
     18 #               expires 12h;
     19                 access_log off;
     20         }
     21 
     22         access_log /tmp/test.com.log;
     23 }
[root@localhost test.com]# /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@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost test.com]# curl -x 127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 04 Jan 2018 08:57:48 GMT
Content-Type: application/x-javascript
Content-Length: 3
Last-Modified: Thu, 04 Jan 2018 08:50:53 GMT
Connection: keep-alive
ETag: "5a4deaed-3"
Accept-Ranges: bytes


转载于:https://my.oschina.net/u/3892756/blog/3069407

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值