LNMP搭建11:Nginx配置防盗链

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP搭建11:Nginx配置防盗链相关的知识,希望对你有一定的参考价值。

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

技术分享

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

技术分享

技术分享

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

编辑虚拟主机配置文件

[[email protected] vhosts]# vim test.conf

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

技术分享

没错误重新加载配置文件

[[email protected] 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

[[email protected] vhosts]# /usr/local/nginx/sbin/nginx -s reload

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

[[email protected] 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

正常情况下是可以访问的

[[email protected] 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

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

[[email protected] 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

[[email protected] 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

其他不能访问我们的图片

[[email protected] 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防盗链Nginx访问控制解析php

12.13 Nginx防盗链;12.14 Nginx访问控制;12.15 Nginx解析php相关配

Nginx学习之如何搭建文件防盗链服务

12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配

Nginx学习之如何搭建文件防盗链服务

nginx防盗链