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