Sunday, August 18, 2019

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提供。

Related Post

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.