Nginx静态文件不记录日志和过期时间和防盗链
Nginx静态文件不记录日志和过期时间
静态文件不记录日志和过期时间
1.修改虚拟主机配置文件
[root@antong ~]# 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 test;
}
[root@antong ~]# /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@antong ~]# /usr/local/nginx/sbin/nginx -s reload
2.模拟一个图像文件
[root@antong ~]# vim /data/wwwroot/test.com/1.jpg
[root@antong ~]# vim /data/wwwroot/test.com/2.js
3.测试
[root@antong ~]# curl -x127.0.0.1:80 test.com/1.jpg
adasd
[root@antong ~]# curl -x127.0.0.1:80 test.com/2.js
sdasd
[root@antong ~]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@antong ~]# curl -x127.0.0.1:80 test.com/2.jshad
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
[root@antong ~]# cat /tmp/test.com.log
127.0.0.1 - [06/Sep/2021:03:54:57 -0400] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [06/Sep/2021:03:57:25 -0400] test.com "/2.jshad" 404 "-" "curl/7.29.0"
//没有记录1.jpg和2.js的访问记录日志
Nginx防盗链
防盗链配置
防盗链需要和上文的静态文件不记录日志不记录时间结合一起,都用到了location
1.修改虚拟主机
server
{
listen 80;
server_name test.com test1.com test2.com;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
}
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d; //以上结尾过期时间7天
valid_referers none blocked server_names *.test.com ; //防盗链部分,定义一个白名单
if ($invalid_referer) { //不在白名单,返回403
return 403;
}
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/test.com.log test;
}
[root@antong ~]# /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@antong ~]# /usr/local/nginx/sbin/nginx -s reload
2.测试
[root@antong ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 test.com/1.jpg -I
HTTP/1.1 403 Forbidden
Server: nginx/1.17.8
Date: Mon, 06 Sep 2021 09:21:21 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@antong ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 test.com/1.jpg -I
HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Mon, 06 Sep 2021 09:22:25 GMT
Content-Type: image/jpeg
Content-Length: 8
Last-Modified: Mon, 06 Sep 2021 07:51:48 GMT
Connection: keep-alive
ETag: "6135c894-8"
Expires: Mon, 13 Sep 2021 09:22:25 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes