静态文件过时缓存、Nginx防盗链、访问控制

防盗链

盗链

  • 盗链是指服务提供商本身不提供服务的内容,经过技术手段绕过其它有利益的最终用户界面(如广告),直接在本身的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不提供资源或提供不多的资源,而真正的服务提供商却得不到任何的收益。

防盗链

  • 要实现防盗链,咱们就必须先理解盗链的实现原理,提到防盗链的实现原理就不得不从HTTP协议提及,在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿连接到当前的网页或文件。换句话说,经过referer,网站能够检测目标网页访问的来源网页,若是是资源文件,则能够跟踪到显示它的网页地址。有了referer跟踪来源,就能够经过技术手段来进行处理,一旦检测到来源不是本站即进行阻止或者返回指定的页面。

防盗链实例

  • 设定目录访问受限,配置blog.abc.com网站目录的访问受限,编辑虚拟主机配置文件
vi /etc/nginx/conf.d/bbs.aaa.com.conf
  • 添加以下内容
location ~ \.(png|gif|jpeg|bmp|mp3|mp4|flv)$
    {
        valid_referers none blocked server_names *.aaa.com;
        if ($invalid_referer) {
                return 403;
        }
    }
  • 测试配置文件是否有错误,并从新加载配置文件
[root@localhost blog.abc.com]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost blog.abc.com]# nginx -s reload
  • 给blog.abc.com上传一个1.jpeg的图片
  • 使用curl -e 命令来模拟referer测试防盗链是否成功
[root@localhost blog.abc.com]# curl -e "http://wwww.baidu.com" -x127.0.0.1:80 blog.abc.com/1.jpeg -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.2
Date: Sun, 17 Feb 2019 12:43:02 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@localhost blog.abc.com]# curl -e "http://bbs.aaa.com" -x127.0.0.1:80 blog.abc.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Sun, 17 Feb 2019 12:48:58 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.3.2
Link: <http://blog.abc.com/index.php?rest_route=/>; rel="https://api.w.org/"

Nginx访问控制

当咱们的网站中有某一站点只是针对公司内部使用,禁止外网使用的时候能够使用访问控制来实现php

  1. 编辑虚拟主机配置文件html

    # vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加以下内容nginx

    allow 127.0.0.1;   //现实生产中,该白名单地址应设置为公司外网地址。
             deny all;
  3. 使用curl命令测试,能够看到,使用指定白名单ip能够正常访问,使用没指定过的ip访问该站点就会受到限制。vim

    # curl -x127.0.0.1:80 test.com/admin/1.jpg
     fangwen kongzhi ceshi `
     # curl -x192.168.254.131:80 test.com/admin/1.jpg
     <html>
     <head><title>403 Forbidden</title></head>
     <body bgcolor="white">
     <center><h1>403 Forbidden</h1></center>
     <hr><center>nginx/1.15.3</center>
     </body>
     </html>

设定指定目录下的PHP文件解析受限

  1. 编辑虚拟主机配置文件api

    # vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加内容网络

    location ~ .*(upload|image)/.*\.php$
     {
     deny all;
     }
  3. 在test.com目录下建立一个upload目录,并写一个PHP文件1.phpcurl

  4. 测试配置文件是否有问题,并从新加载ide

    # /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[@localhost](https://my.oschina.net/u/570656) ~]# /usr/local/nginx/sbin/nginx -s reload
  5. 使用curl测试限制解析是否成功,能够看到返回的代码是403,表示限制解析成功测试

    [root[@localhost](https://my.oschina.net/u/570656) ~]# curl -x127.0.0.1:80 test.com/upload/1.php 
     <html>
     <head><title>403 Forbidden</title></head>

根据user_agent限制访问(能够拒绝网络爬虫爬走网站内容)

好比我想让谁访问个人网站,我就告诉他域名,若是不告诉别人域名,就说明我不想让他知道个人站点,这须要禁止搜索引擎在网络上爬取站点内容。能够经过user_agent来限制。网站

  1. 编辑虚拟主机文件

    [root[@localhost](https://my.oschina.net/u/570656) ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加以下内容

    if ($http_user_agent ~* 'Spider/3.0|baidu|YoudaoBot|Tomato')
     {
     return 403;
     }
  3. 测试并从新加载配置文件 ..-t ...-s reload

  4. 使用curl测试,curl -A 能够模拟user_agent,发现返回的代码是403,表示实验成功。

    [root[@localhost](https://my.oschina.net/u/570656) ~]# curl -A "www.baidu.com" -x127.0.0.1:80 test.com -I
     HTTP/1.1 403 Forbidden
     Server: nginx/1.15.3
     Date: Tue, 04 Sep 2018 17:57:37 GMT
     Content-Type: text/html
     Content-Length: 169
     Connection: keep-alive
相关文章
相关标签/搜索