首页 > 代码库 > Nginx 配置静态文件过期时间&防盗链
Nginx 配置静态文件过期时间&防盗链
[root@LNMP ~]# vim /usr/local/nginx/conf/vhosts/linux.conf
location ~ .*\.(gif|jpeg|jpg|png|bmp|swf)$
{
access_log off;
expires 1d; #过期时间 1天
}
location ~ .*\.(js|css) #此也也可改为 \.(js|css)来匹配
{ access_log off;
expires 2h;
}
[root@LNMP ~]# /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@LNMP ~]# /usr/local/nginx/sbin/nginx -s reload #重新加载
[root@LNMP ~]#
[root@LNMP ~]# curl -x 127.0.0.1:80 ‘http://www.linux.com/static/image/smiley/default/smile.gif‘ -I
配置缓存的目的,提高访问的速度,节省带宽。
-----------分割线-------------------------------------------------
Nginx 防盗链配置 2016-12-02 15:20
[root@LNMP ~]# vim /usr/local/nginx/conf/vhosts/linux.conf
location ~ .*\.(gif|jpeg|jpg|png|bmp|swf|flv|rar|zip|gz|bz2)$
{
access_log off;
expires 1d;
valid_referers none blocked *.linux.com *.test.com; #访问白明单
if ($invalid_referer)
{
return 403; #返回 403
}
#红色字体是新加的防盗链,配置。
测试如下图
以下是白明单访问
本文出自 “CBO#Boy_Linux之路” 博客,请务必保留此出处http://20151213start.blog.51cto.com/9472657/1878896
Nginx 配置静态文件过期时间&防盗链