Thursday, November 21, 2024

Nginx设置图片防盗链

在/usr/local/nginx/conf/nginx.conf文件要添加防盗链的server段里添加下面的代码:

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
          {
                  valid_referers none blocked *.vpser.net *.licess.org *.licess.cn;
                                if ($invalid_referer) {
                                rewrite ^/ http://linode.vpser.net/404.jpg;
                                #return 404;
                                }
          }

然后执行如下命令重启lnmp即可:

/root/lnmp restart

说明:
1、请将代码中的*.vpser.net *.licess.org *.licess.cn等换成你自己的域名。
2、请确保server段中只有一个location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$,否则可能导致代码无效。

如果有下面这段代码,请将其删除或者与上面的代码合并成一段:

		location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
			{
				expires      30d;
			}

再次重启lnmp,你会发现,防盗链设置已经生效了。

本文代码由Licess提供。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.