Nginx优化之防盗链

Nginx优化之防盗链

文章目录

  • Nginx优化之防盗链
    • 前言
    • 实验环境
    • 实验步骤

前言

之前,我们知道了Apache的防盗链,接下来我们来看看nginx的防盗链。

有时候在浏览网页的时候,会遇到某些文件(图片等)无法访问的情况,这是因为图片的所有方做了防盗链机制

了解防盗链之前先了解下http referer这个属性,http referer是请求头中的一部分,当浏览器向web服务器发出请求时,一般会带上这个属性用来表明网页的来源,比如我在qq空间里添加朋友的空间链接,那么当有人点击我空间里的这个链接调到朋友的qq空间时,referer的值就是我空间的url。

防盗链的基本原理就是根据请求头中referer属性得到网页来源,从而实现访问控制。

为什么要实现防盗链?首先这些非法访问并不会给网站带来利益或好处,相反,这会浪费网站的带宽,增加服务器的连接压力,比如有些网站是按流量收费的,那么只要有人访问了盗用图片或其他文件的网站,网站就要支付这部分的流量费用。

实验环境

centos7虚拟机两台

win10测试主机一台

实验步骤

1.手工编译安装nginx(两台主机都要装)

我们将主服务器的名称改为nginx 盗链的服务器的主机名称改为盗链

主服务器
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# su
[root@nginx ~]# 
盗链
[root@localhost ~]# hostnamectl set-hostname daolian
[root@localhost ~]# su
[root@daolian ~]# 

下面我们以安装nginx主服务器及优化,盗链服务器我们就安装简易Apache服务

[root@nginx ~]# ls
anaconda-ks.cfg       nginx-1.12.2.tar.gz  模板  图片  下载  桌面
initial-setup-ks.cfg  公共                 视频  文档  音乐
[root@nginx ~]# tar -zxvf nginx-1.12.2.tar.gz -C /opt/
[root@nginx ~]# cd /opt
[root@nginx opt]# ls
nginx-1.12.2  rh
[root@nginx opt]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.2]# yum -y install pcre-devel zlib-devel make gcc gcc-c++  bind
[root@nginx nginx-1.12.2]# rpm -q gcc
gcc-4.8.5-39.el7.x86_64
[root@nginx nginx-1.12.2]# rpm -q gcc-c++
gcc-c++-4.8.5-39.el7.x86_64
[root@nginx nginx-1.12.2]# rpm -q pcre
pcre-8.32-17.el7.x86_64
[root@nginx nginx-1.12.2]# rpm -q pcre-devel
pcre-devel-8.32-17.el7.x86_64
[root@nginx nginx-1.12.2]# rpm -q zlib-devel
zlib-devel-1.2.7-18.el7.x86_64
[root@nginx nginx-1.12.2]# rpm -q make
make-3.82-24.el7.x86_64
[root@nginx nginx-1.12.2]# rpm -q bind
bind-9.11.4-9.P2.el7.x86_64
[root@nginx nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@nginx nginx-1.12.2]# make && make install

盗链网站

[root@daolian ~]# yum -y install httpd bind
[root@daolian ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64
[root@daolian ~]# rpm -q bind
bind-9.11.4-9.P2.el7.x86_64

2.优化nginx

[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.2]# 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@nginx nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20 
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in 
  start) 
      $PROG
          ;;
  stop)
      kill -s QUIT $(cat $PIDF)
          ;;
  restart)
      $0 stop
          $0 start
          ;;
  reload)
      kill -s HUP $(cat $PIDF)
          ;;
  *)
      echo "Usage: $0 {
     start|stop|restart|reload}"
      exit 1
esac
exit 0
[root@nginx nginx-1.12.2]# cd /etc/init.d/
[root@nginx init.d]# ls
functions  netconsole  network  nginx  README
[root@nginx init.d]# chmod +x nginx
[root@nginx init.d]# ls
functions  netconsole  network  nginx  README
[root@nginx init.d]# chkconfig --add nginx
[root@nginx init.d]# chkconfig --level 35 nginx on
[root@nginx init.d]# service nginx start
[root@nginx init.d]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7594/nginx: master

3.将图片导入到nginx的站点下面

[root@nginx init.d]# cd /usr/local/nginx
[root@nginx nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@nginx nginx]# cd html/
[root@nginx html]# ls
50x.html  index.html
[root@nginx html]# ls
1.gif  2.png  50x.html  index.html
[root@nginx html]# vim index.html
"1.gif" \>

在盗链里面将图片盗走

[root@daolian ~]# cd /var/www/html/
[root@daolian html]# ls

this is dao lian

"http://www.hello.com/1.gif" \ >

4.开启服务器nginx的dns域名解析服务

[root@nginx html]# vim /etc/named.conf
        listen-on port 53 {
      any; };
        allow-query     {
      any; };
[root@nginx html]# vim /etc/named.rfc1912.zones
zone "hello.com" IN {
     
        type master;
        file "hello.com.zone";
        allow-update {
      none; };
};
[root@nginx html]# cd /var/named
[root@nginx named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@nginx named]# cp -p named.localhost hello.com.zone
[root@nginx named]# ls
data  dynamic  hello.com.zone  named.ca  named.empty  named.localhost  named.loopback  slaves
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.73.223
[root@nginx named]# vim /usr/local/nginx/conf/nginx.conf
            server {
     
        listen       80;
        server_name  www.hello.com;
[root@nginx named]# systemctl restart named
[root@nginx named]# service nginx stop
[root@nginx named]# service nginx start
[root@nginx named]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      42322/nginx: master
[root@nginx named]# systemctl stop firewalld
[root@nginx named]# setenforce 0

开启盗链主机的Apache服务

[root@daolian html]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.73.175:80
#Listen 80
ServerName www.world.com:80
[root@daolian html]# systemctl stop firewalld
[root@daolian html]# setenforce 0
[root@daolian html]# systemctl restart httpd
[root@daolian html]# netstat -ntap|grep 80
tcp        0      0 192.168.73.175:80       0.0.0.0:*               LISTEN      37992/httpd

5.测试在win10主机里面是否能够访问

我们要将域名服务器的地址写入win10主机的dns里面

Nginx优化之防盗链_第1张图片

在盗链服务器中

Nginx优化之防盗链_第2张图片

6.修改服务器配置文件

[root@nginx named]# vim /usr/local/nginx/conf/nginx.conf
		location ~*\.(jpg|gif|swf)$ {
     
        valid_referers none blocked *.hello.com hello.com;
        if ($invalid_referer) {
     
            rewrite ^/ http://www.hello.com/2.png;
        }
        }
[root@nginx named]# service nginx stop
[root@nginx named]# service nginx start

7.在win10主机里面测试结果

Nginx优化之防盗链_第3张图片

Nginx优化之防盗链_第4张图片

你可能感兴趣的:(云计算架构,nginx,优化)