码迷,mamicode.com
首页 > 其他好文 > 详细

LNMP搭建11:Nginx配置防盗链

时间:2017-02-25 21:59:11      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:lnmp   搭建   nginx   配置   防盗链   

我们网站上的资源如果没有设置防盗链,那么其他人可以通过资源链接盗用我们的资源,占用我们的带宽,影响我们网站对合法用户的服务质量。举个例子,假如我们网站上有一张图片,如下图所示:

技术分享

复制图片地址,可以直接在浏览器中搜到我们的图片,也可以在其他地方通过该图片地址引用或下载该图片。

技术分享

技术分享

为了不让别人盗用我们的资源,我们可以在服务器上设置防盗链。

编辑虚拟主机配置文件

[root@cp1 vhosts]# vim test.conf

添加指定类型文件的防盗链

技术分享

没错误重新加载配置文件

[root@cp1 vhosts]# /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@cp1 vhosts]# /usr/local/nginx/sbin/nginx -s reload

使用curl测试,看看百度能不能引用我们的图片

[root@cp1 vhosts]# curl -e "http://www.baidu.com" -I -x127.0.0.1:80 ‘http://www.test.com/data/attachment/forum/201702/24/020448ytywyltbbzatdgdo.jpg‘

HTTP/1.1 403 Forbidden

Server: nginx/1.6.2

Date: Thu, 23 Feb 2017 18:24:47 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

正常情况下是可以访问的

[root@cp1 vhosts]# curl -I -x127.0.0.1:80 ‘http://www.test.com/data/attachment/forum/201702/24/020448ytywyltbbzatdgdo.jpg‘

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Thu, 23 Feb 2017 18:22:34 GMT

Content-Type: image/jpeg

Content-Length: 188486

Last-Modified: Thu, 23 Feb 2017 18:04:49 GMT

Connection: keep-alive

ETag: "58af2441-2e046"

Expires: Fri, 10 Mar 2017 18:22:34 GMT

Cache-Control: max-age=1296000

Accept-Ranges: bytes

指定的白名单能访问我们的图片

[root@cp1 vhosts]# curl -e "http://www.test.com" -I -x127.0.0.1:80 ‘http://www.test.com/data/attachment/forum/201702/24/020448ytywyltbbzatdgdo.jpg‘

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Thu, 23 Feb 2017 18:25:24 GMT

Content-Type: image/jpeg

Content-Length: 188486

Last-Modified: Thu, 23 Feb 2017 18:04:49 GMT

Connection: keep-alive

ETag: "58af2441-2e046"

Expires: Fri, 10 Mar 2017 18:25:24 GMT

Cache-Control: max-age=1296000

Accept-Ranges: bytes

[root@cp1 vhosts]# curl -e "http://www.aaa.com" -I -x127.0.0.1:80 ‘http://www.test.com/data/attachment/forum/201702/24/020448ytywyltbbzatdgdo.jpg‘

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Thu, 23 Feb 2017 18:25:32 GMT

Content-Type: image/jpeg

Content-Length: 188486

Last-Modified: Thu, 23 Feb 2017 18:04:49 GMT

Connection: keep-alive

ETag: "58af2441-2e046"

Expires: Fri, 10 Mar 2017 18:25:32 GMT

Cache-Control: max-age=1296000

Accept-Ranges: bytes

其他不能访问我们的图片

[root@cp1 vhosts]# curl -e "http://www.google.com" -I -x127.0.0.1:80 ‘http://www.test.com/data/attachment/forum/201702/24/020448ytywyltbbzatdgdo.jpg‘

HTTP/1.1 403 Forbidden

Server: nginx/1.6.2

Date: Thu, 23 Feb 2017 18:24:24 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive


LNMP搭建11:Nginx配置防盗链

标签:lnmp   搭建   nginx   配置   防盗链   

原文地址:http://rachy.blog.51cto.com/11428504/1901171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!