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
<img src="1.gif" \>

在盗链里面将图片盗走

[root@daolian ~]# cd /var/www/html/
[root@daolian html]# ls
<h1>this is dao lian </h1>
<img src="http://www.hello.com/1.gif" \ >

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

[root@nginx html]# vim /etc/named.conflisten-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       ; serial1D      ; refresh1H      ; retry1W      ; expire3H )    ; minimumNS      @A       127.0.0.1
www IN  A       192.168.73.223
[root@nginx named]# vim /usr/local/nginx/conf/nginx.confserver {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里面

在盗链服务器中

6.修改服务器配置文件

[root@nginx named]# vim /usr/local/nginx/conf/nginx.conflocation ~*\.(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优化之防盗链相关推荐

  1. Nginx优化与防盗链

    Nginx优化与防盗链 一.隐藏nginx版本号 1.1查看版本号 1.2隐藏版本信息 二.修改用户与组 三.缓存时间 四.日志分割 五.连接超时 六.更改进程数 七.配置网页压缩 八.配置防盗链 8 ...

  2. 学会Nginx优化与防盗链预防坏蜀黍

    Nginx优化与防盗链 文章目录 一.Nginx优化 1.配置nginx隐藏版本号 修改配置文件 修改源码文件,重新编译安装 2.修改用户与组 3.缓存时间 4.日志切割 5.连接超时 6.更改进程数 ...

  3. Nginx优化与防盗链(隐藏版本号、配置缓存时间、日志分割、修改进程数、配置连接超时、使用gzip压缩页面、防盗链设置,fpm优化)

    文章目录 隐藏Nginx版本号 网页压缩 网页压缩配置 网页缓存时间 网页缓存时间设置 更改Nginx运行进程数 连接超时 nginx防盗链设置 盗链网站 配置httpd 日志分割 fpm参数优化 隐 ...

  4. 九爷 带你了解 nginx优化与防盗链

    Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为"engine X",是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP  ...

  5. Nginx 优化与防盗链

    文章目录 一.Nginx 优化 1. 隐藏版本号 (1) 隐藏版本号的原因 (2) 版本号查看 ① nginx -v (仅限 web 浏览器) ② curl -I ③ 浏览器查看 (3) 隐藏方法 ① ...

  6. centos 7.6 —— Nginx 配置网页防盗链FPM参数优化

    centos 7.6 -- Nginx 配置网页防盗链&&FPM参数优化 一.网页防盗链 (1)防盗链端--服务端配置(192.168.75.134) 1.1 服务端配置DNS服务,域 ...

  7. Nginx网页优化及防盗链__gw

    Nginx网页优化及防盗链 Nginx服务优化 配置Nginx隐藏版本号 方法一 方法二 修改nginx用户和组 网页缓存时间 Nginx的日志分割 编写日志分割脚本 制作任务计划 链接超时 更改Ng ...

  8. Nginx使用http_accesskey_module防盗链,告别referer,告别伪装

    Nginx使用http_accesskey_module防盗链,告别referer,告别伪装 传统的防盗链很多是依赖referer,比如apache的rewrite模块,根据规则判断referer,这 ...

  9. Nginx设置图片防盗链(白名单与黑名单)

    点击蓝字关注这个神奇的公众号- 某些时候可能您会发现,别人网站直接将您的网站图片拿过去使用,导致额外消耗服务器流量和带宽,如果本身服务器带宽和流量就比较小,被人盗链后势必会造成一定影响.这篇文章分享下 ...

最新文章

  1. Linux 交换空间管理和技巧
  2. Android Studio通过JNI调用NDK程序
  3. 多线程终极模式:生产者-消费者模式
  4. NumPy的思考……
  5. c语言pta按等级统计学生成绩,第九周作业
  6. Java ObjectInputStream readUnsignedShort()方法(带示例)
  7. Java核心类库篇3——util
  8. linux eclipse gtk,Ubuntu+Eclipse下开发GTK+应用程序
  9. glyphicons-halflings-regular.woff2 文件 404
  10. 抖音点赞最多的标题_抖音吸引人评论的标题,抖音什么标题容易点赞
  11. vector扩容时以2倍或1.5倍扩容的原因
  12. Hibernate中类的继承使用subclass实现
  13. 【studio】整理了下studio中make Project、clean Project、Rebuild Project的区别
  14. IE浏览器无法打开HTTPS解决办法
  15. 吴恩达机器学习ex4 Neural Networks Learning (python)
  16. oenwrt 进不了bios_J1900在openwrt不能正常重启的BIOS选项说明
  17. 百度地图Javascript API 使用记录
  18. qurlinfo在qt5中_QT5编译使用QFtp的方法步骤
  19. Navicat 快捷键大全
  20. Python 取模运算(取余)%误区及详解

热门文章

  1. 小程序毕业设计 基于java后台微信餐厅座位预定小程序毕业设计参考
  2. 非计算机专业进入IT行业的三个方法,你是哪一个?
  3. Python数据结构——图(graph)
  4. Controller
  5. java实现微信、手机号登陆_微信小程序获取手机号授权用户登录功能
  6. 新版Chrome如何更换搜索引擎
  7. 微信海外版 服务器,node-weixin开源微信API与服务器项目新版本发布
  8. [c语言]小课堂 day5
  9. 《微信》微信商户提现到零钱
  10. Python 提取前景-单一颜色背景