Nginx 动静分离 防盗链。

一、动静分离

          环境准备

   静态资源配置(10.36.192.137)

          安装nginx

         修改配置文件

         重启nginx      

  动态资源配置 (10.36.192.138)

          安装php

        修改nginx配置文件

         重启nginx

   nginx代理机配置(10.36.192.139)

      修改nginx子配置文件 

            重启nginx

        客户端访问

二、防盗链

nginx 防止网站资源被盗用模块

 如何区分哪些是不正常模块

盗链实验

  在正版网站发布资源

   浏览器访问

浏览器访问盗版网站

观察正版服务器的日志

防盗链实验

修改正版网站的配置文件

none 字段

server_name字段

一、动静分离

实质:使用正则表达式,匹配过滤,交给不同的服务器

优点:把动态页面和静态页面分别由不同的服务器来解析,加快服务器速度,缓解单个服务器压力

环境准备

静态资源管理 (10.36.192.137)

echo '
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true ' > /etc/yum.repos.d/nginx.repo
yum clean all
yum makecache
yum repolist
yum install yum-utils
yum -y install nginx

 修改配置文件

vim /etc/nginx/conf.d/default.conf
server {
        listen 80;
        server_name     localhost;

        location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) {
        root /usr/share/nginx/html;
        }
}

重启nginx

nginx -t
nginx -s reload

 动态资源配置 (10.36.192.138)

yum安装php

rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
yum install -y php71w-fpm
systemctl start php-fpm
systemctl enable php-fpm

yum 安装nginx方法如上 

修改nginx配置文件
vim /etc/nginx/conf.d/default.conf
server {
        listen      80;
        server_name     localhost;
        location ~ \.php$ {
            root           /usr/local/nginx/html;  #指定网站目录
            fastcgi_pass   127.0.0.1:9000;    #指定访问地址
            fastcgi_index  index.php;		#指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
        		}
        }

 重启nginx

nginx -t
nginx -s reload

 nginx代理机配置 (10.36.192.139)

修改nginx子配置文件

vim /etc/nginx/conf.d/default.conf
upstream static  {
  server 10.36.192.137   weight=1  max_fails=2  fail_timeout=2s;
}
upstream php {
  server 192.168.20.138 weight=2  max_fails=2  fail_timeout=2s;              
}

server {

   listen       80;
   server_name  localhost;
   location ~ \.php$ {
           proxy_pass http://php;
           proxy_set_header Host $host:$server_port;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               }
location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
           proxy_pass http://static;
           proxy_set_header Host $host:$server_port;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
}

 重启nginx

nginx -t
nginx -s reload

 二、防盗链

盗链 :两个网站A和B,A网站引用了B网站上的资源,这种行为叫做盗链
防盗链 : 防止A引用B的资源

 nginx防止网站资源被盗用模块

ngx_http_referer_module

如何区分哪些是不正常用户

HTTP Referer是header的一部分,当浏览器向web服务器发送请求时,一般会带上Referer,告诉服务器我从那个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许的网站盗链图片,文件等。HTTP Referer可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,他能够限制大部分的盗链情况。

 盗链实验

服务器1IP: 192.168.20.135 (正版网站)
服务器2IP: 192.168.0.12

 在正版网站发布资源

修改配置文件(发布图片1.jpg)
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 
 
server {
    listen  80;
    server_name  localhost;
    location /{
    root  /usr/share/nginx/html; #网站默认发布路径
     index  1.jpg;
    
}
}
重启nginx服务
nginx -t
nginx -s reload

 浏览器访问 192.168.20.135

盗版网站盗用

修改网站发布页面
[root@daili ~]# cd /usr/share/nginx/html/ #yum安装nginx的默认发布路径
[root@daili html]# vim index.html
<html>
<head>
    <meta charset="utf-8">    
    <title>qf.com</title>
</head>
<body style="background-color:green;">     #背景为绿色
    <img src="http://192.168.20.135/1.jpg"/>    #盗用171IP的1.jpg这个图片
</body>
</html>

 浏览器访问盗版网站 192.168.0.12

观察正版服务器的日志  tailf  /var/log/access.log

防盗链实验

服务器1IP: 192.168.20.135 (正版网站)
服务器2IP: 192.168.0.12

 

 

 none:允许没有http_Referer请求访问资源

blocked:允许不是http://开头的,不带协议请求访问资源;

server_names:只允许访问ip/域名来的请求访问资源(白名单)

 

 修改正版网站的子配置文件

vim /etc/nginx/conf.d/default.conf 
 
server {
    listen  80;
    server_name  localhost;
    location /{
    root  /usr/share/nginx/html;
     index  1.jpg;
valid_referers  none  blocked www.baidu.com;   
 
if ($invalid_referer) {
 return 502;             
}
 
}
}

重启服务
nginx -t 
nginx -s reload

开启防盗链

nono字段

无nono访问正版网站,也就是Referer为空,访问不到正版服务器的资源

server {

   listen       80;
   server_name  localhost;

location / {
        root   /usr/share/nginx/html;
        index  1.jpg;

       valid_referers   blocked   192.168.0.12;
             if ($invalid_referer) {
                  return 502;
              }
       }
}

 server_name字段

如果在服务器上将做了将盗链的机器ip写入白名单(server_names),这样就可以访问到了,状态码为200

server {

    listen       80;
    server_name  localhost;

location / {
         root   /usr/share/nginx/html;
         index  1.jpg;

        valid_referers  none blocked   192.168.0.12;
              if ($invalid_referer) {
                   return 502;
               }
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值