[[email protected] ~]# 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;
}
[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload #重新加载
[[email protected] ~]#
[[email protected] ~]# curl -x 127.0.0.1:80 ‘http://www.linux.com/static/image/smiley/default/smile.gif‘ -I
配置缓存的目的,提高访问的速度,节省带宽。
-----------分割线-------------------------------------------------
Nginx 防盗链配置 2016-12-02 15:20
[[email protected] ~]# 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
}
#红色字体是新加的防盗链,配置。
测试如下图
以下是白明单访问
Nginx 配置静态文件过期时间&防盗链