Nginx优化之压缩和防盗链
admin
2023-03-29 00:41:31
0

Nginx优化之压缩和防盗链

Nginx优化之压缩

配置nginx

[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安装环境包

[root@localhost ~]# useradd -M -s /sbin/nologin nginx  ##创建程序性用户

[root@localhost ~]# mkdir /chen  ##创建挂载点
[root@localhost ~]# mount.cifs //192.168.100.23/LNMP /chen  ##挂载
Password for root@//192.168.100.23/LNMP:  

[root@localhost chen]# tar zxvf nginx-1.12.2.tar.gz -C /opt/  ##解压

[root@localhost chen]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

./configure \  ##安装nginx组件
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

[root@localhost nginx-1.12.2]# make && make install ##编译

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##做软链接让系统能识别nginx的所有人命令
[root@localhost 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

写nginx脚本放在系统启动脚本中方便service管理器管理

[root@localhost nginx-1.12.2]# cd /etc/init.d/ ##到系统启动脚本

[root@localhost init.d]# vim nginx   ##写一个nginx脚本

#!/bin/bash
#chkconfig: - 99 20  #注释信息
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"  #这个变量,指向我的命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"  #这个变量,指向nginx的进程号
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@localhost init.d]# chmod +x nginx  ##给Nginx提升权限
[root@localhost init.d]# chkconfig --add nginx  ##添加nginx
[root@localhost init.d]# service nginx start 
[root@localhost init.d]# netstat -ntap | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17544/nginx: master 

[root@localhost init.d]# systemctl stop firewalld.service
[root@localhost init.d]# setenforce 0

nginx优化之压缩配置

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

gzip on;  ##开启压缩功能
gzip_min_length 1k;  ##超过1kb就会压缩
gzip_buffers 4 16k;  ##缓存空间,大小为4个16k
gzip_http_version 1.1;  ##压缩版本
gzip_comp_level 6;  ##压缩比率,最小为1,处理速度快,传输慢,9最大压缩比,处理速度慢,传输快,适中我们选5或6
gzip_types test/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml test/javascript application/x-httpd-php application/javascript application/json;
##支持这些格式压缩
gzip_disable "MSIE [1-6]\."; ##配置禁用gzip条件,支持正则,ie6浏览器以下不启用gzip
gzip_vary on;  ##对压缩的页面可以缓存到前端的服务器当中

我们找一张图片来测试压缩功能

[root@localhost ~]# cd /usr/local/nginx/html/ ##到nginx站点中
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mount.cifs //192.168.100.23/LNMP /mnt ##挂载
Password for root@//192.168.100.23/LNMP:  
[root@localhost html]# cd /mnt/
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.20.tar.gz
fang.png                   nginx-1.12.2.tar.gz  shu.jpg
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2

[root@localhost html]# cp /mnt/shu.jpg ./ ##到挂载目录
[root@localhost html]# ls
50x.html  index.html  shu.jpg

[root@localhost html]# vim index.html  ##到站点网页,写入图片路径
15 

[root@localhost html]# service nginx stop ##关闭nginx
[root@localhost html]# service nginx start  ##开启nginx

[root@localhost html]# systemctl stop firewalld.service  ##关闭防火墙
[root@localhost html]# setenforce 0  ##关闭增强功能

Nginx优化之防盗链

实验环境,Nginx服务器,win10-1作为测试访问,win10-2作为盗链网址

先将win10-2做一个网站,写一个默认网页,其中图片的路径是盗链Nginx的图片

Nginx优化之压缩和防盗链

文件扩展名改成index.html

Nginx优化之压缩和防盗链

win10-2安装网站程序

Nginx优化之压缩和防盗链

打开网站程序,默认是开启的

Nginx优化之压缩和防盗链

Nginx优化之压缩和防盗链

把你刚才写的页面放到站点中

Nginx优化之压缩和防盗链

回到Nginx服务器安装DNS服务并配置

[root@localhost html]yum install bind -y  ##安装DNS软件包
[root@localhost html]# vim /etc/named.conf  ##配置主配置文件
options {
        listen-on port 53 { any; };  ##监听所有地址
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };  ##允许所有人可以访问
[root@localhost html]# vim /etc/named.rfc1912.zones  ##配置区域配置文件

zone "kgc.com" IN {        type master;  ##定义kgc.com域名
        file "kgc.com.zone";  ##定义区域数据配置文件
        allow-update { none; };
};
[root@localhost html]# cd /var/named/
[root@localhost named]# cp -p named.localhost kgc.com.zone ##复制模板到创建的新区域数据配置文件中
[root@localhost named]#vim kgc.com.zone ##编辑数据区域数据配置文件
$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.136.163  ##主机名,地址

[root@localhost named]# systemctl start named  ##开启dns服务

win10-1和win10-2都要选择DNS去解析

Nginx优化之压缩和防盗链

win10-2测试解析

Nginx优化之压缩和防盗链

win10-1去访问win10-2的网址

Nginx优化之压缩和防盗链

访问域名还是这个图片

Nginx优化之压缩和防盗链

回到Nginx做防盗链配置

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
 66         location ~*\.(jpg|gif|swf)$ {  ##带有这些格式为结尾的图片信息
 67             valid_referers none blocked *.kgc.com kgc.com; ##本地解析的就会去跳转相应的图片
 68             if ( $invalid_referer ) {  ##如果你不是本地解析的,以盗链访问
 69                rewrite ^/ http://www.kgc.com/fang.png;  ##就会跳转防盗链的图片给你
 70  }
  71    }
[root@localhost html]# service nginx stop

[root@localhost html]# service nginx start

再去win10-1测试访问一下

Nginx优化之压缩和防盗链

以上就是我们全部的内容了,谢谢收看

相关内容

热门资讯

今日重大消息“一起温州麻将有没... 家人们!今天小编来为大家解答一起温州麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
中国联通赋能全栈国产化智算中心... 来源:人民邮电报 本报讯 近日,国内规模领先的医疗领域全栈国产化智算中心在北京次渠正式点亮,首期38...
最新引进“蜀友汇到底有挂吗?”... 最新引进“蜀友汇到底有挂吗?”(确实真的有挂)您好,蜀友汇这个游戏其实有挂的,确实是有挂的,需要了解...
原创 帮... 浩瀚宇宙,危机四伏。人类习惯了抬头仰望那轮皎洁的明月,将其视为寄托相思与浪漫的图腾,却往往忽略了它最...
玩家分享攻略“飞鹰炸/金/花是... 网上科普关于“飞鹰炸/金/花有没有挂”话题很是火热,小编也是针对飞鹰炸/金/花作*弊开挂的方法以及开...
最新引进“相约福建麻将是不是有... 有 亲,根据资深记者爆料相约福建麻将是可以开挂的,确实有挂(咨询软件无需...
今日重磅消息“728开挂神器?... 有 亲,根据资深记者爆料728是可以开挂的,确实有挂(咨询软件无需打开直...
我来教教您“来几局怎么装挂?”... 您好:来几局这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游戏中...
终于了解“宝宝吃吃吃有挂吗?”... 您好:宝宝吃吃吃这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游...
【第一消息】“新蓝鲸拼三张怎么... 您好:新蓝鲸拼三张这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款...