温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx优化--网页压缩与配置防盗链

发布时间:2020-07-20 22:46:18 来源:网络 阅读:12597 作者:caozhengtao1213 栏目:系统运维

配置Nginx实现网页压缩功能

Nginx的ngx http .gzip_ module压缩模块提供对文件内容压缩的功能,允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装.可在配置文件中加入相应的压缩功能参数对压缩性能进行优化

压缩功能参数

gzip on:开启gzip压缩输出

gzip_ min_ length 1k:用于设置允许压缩的页面最小字节数

gzip_ buffers 416k:表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来储gzip压缩结果

zip_ http_ version 1.0:用于设置识别http协议版本,默认是1.1, 目前大部分浏览器已经支持gzip解压,但处理最慢,也比较消耗服务器CPU资源

gzip_ _comp_ level 2:用来指定gzip压缩比,1压缩比最小,处理速度最快; 9压缩比最大,传输速度快,但处理速度最慢,使用默认即可

gzip_ types text/plain:压缩类型,是就对哪些网页文档启用压缩功能

gzip_ vary on:选项可以让前端的缓存服务器缓存经过gzip压缩的页面

网页压缩实例演示

一、编译安装Nginx服务

第一步:远程获取Windows上的源码包,并挂载到Linux上

[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password: 
Sharename       Type      Comment
---------       ----      -------
LNMP            Disk  

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:  
[root@localhost ~]# [root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz
error.png                  nginx-1.12.2.tar.gz
game.jpg                   php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz  php-7.1.20.tar.gz

第二步:解压源码包

[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0  rh

第三步:下载安装编译组件包

[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \             //C语言
> gcc-c++ \         //c++语言
> pcre-devel \      //pcre语言工具
> zlib-devel        //压缩函数库

第四步:创建程序用户并配置Nginx服务相关组件

[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//创建程序用户nginx,并限定其不可登录终端
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \            
//配置nginx
> --prefix=/usr/local/nginx \       
//指定安装路径                        
> --user=nginx \
//指定用户名
> --group=nginx \
//指定用户所属组
> --with-http_stub_status_module
//安装状态统计模块

第五步:编译与安装Nginx

[root@localhost nginx-1.12.0]# make && make install

第六步:优化Nginx服务启动脚本,并建立命令软连接

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
//创建nginx服务命令软链接到系统命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service 
//关闭防火墙
[root@localhost nginx-1.12.0]# setenforce 0
//关闭增强型安全功能
[root@localhost nginx-1.12.0]# nginx 
//输入nginx 开启服务
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80      //查看服务的80 端口,显示已开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7520/nginx: master  

第七步:systemctl管理nginx脚本

[root@localhost ~]# vim /lib/systemd/system/nginx.service      ##创建配置文件

[Unit]
Description=nginx                                            ##描述
After=network.target                                        ##描述服务类型
[Service]
Type=forking                                                    ##后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid            ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx              ##启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID    ##根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID       ##根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service     ##设置执行权限
[root@localhost ~]# systemctl stop nginx.service       ##关闭nginx 
[root@localhost ~]# systemctl start nginx.service       ##开启nginx 

二、修改Nginx.conf文件

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

gzip  on;
#使用x键删除此行前的井号注释

gzip_min_length 1k;
#压缩阈值

gzip_buffers 4 16k;
#buffers大小为4个16k缓冲区大小

gzip_http_version 1.1;
##压缩版本号

gzip_comp_level 6;
#压缩比率,最小为1,处理快但传输慢;最大为9,处理慢,但传输快;此处设6,相对适中

gzip_types text/plain application/x-javascript text/css image/jpg image/jpegimage/png image/gif application/xml text/javascript application/x-httpd-php 
application/javascript application/json;
#支持的类型格式类型

gzip_disable "MSIE [1-6]\.";
#配置禁用gzip条件,支持正则表达式,表示ie6以下不启用gzip

gzip_vary on;
#让前端的缓存服务器缓存经过gzip压缩的页面

三、Nginx网页中放入图片

第一步:复制图片到站点目录

[root@localhost conf]# cd ../html/
[root@localhost html]# cp /abc/game.jpg ./
[root@localhost html]# ls
50x.html  game.jpg  index.html

第二步:修改站点首页内容

[root@localhost html]# vim index.html

<h2>Welcome to nginx!</h2>
<img src="game.jpg"/>
##在h2标签下添加图片路径

[root@localhost html]# systemctl stop nginx.service 
[root@localhost html]# systemctl start nginx.service 
[root@localhost html]# systemctl stop firewalld.service 
[root@localhost html]# setenforce 0

第四步:打开一台Win10虚拟机验证网页图片压缩

在客户机中安装fiddler.exe抓包软件,并打开浏览器访问192.168.235.158网页

Nginx优化--网页压缩与配置防盗链

防盗链实例演示

第一步:安装配置DNS域名解析服务

[root@localhost ~]# yum install bind -y
[root@localhost ~]# vim /etc/named.conf 

options {
        listen-on port 53 { any; };
        ...
        allow-query     { any; };

[root@localhost ~]# vim /etc/named.rfc1912.zones

zone "accp.com" IN {        type master;
        file "accp.com.zone";
        allow-update { none; };
};    

[root@localhost ~]# cd /var/named/ 
[root@localhost named]# cp -p named.localhost accp.com.zone
#复制模板文件

[root@localhost named]# vim accp.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.235.158
#IP地址指向本机地址

[root@localhost named]# systemctl start named 
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0

第二步:打开一台Win7虚拟机,搭建盗链网站

1.创建一个网页内容,修改后缀为index.html

<html>
 <head>
     <title>云计算</title>
 </head>
 <body>
     <h2>this is test web</h2> 
     <img src="http://www.accp.com/game.jpg"/>   
 </body>
</html>

2.搭建网站
Nginx优化--网页压缩与配置防盗链
Nginx优化--网页压缩与配置防盗链3.修改首选DNS服务器地址

Nginx优化--网页压缩与配置防盗链

第三步:分别访问原网页与盗链网页

Nginx优化--网页压缩与配置防盗链
Nginx优化--网页压缩与配置防盗链

第四步:修改Nginx.conf文件

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

location ~*\.(jpg|gif|swf)$ { 
                #支持格式                       
                valid_referers none blocked *.accp.com accp.com;    
                #允许用accp.com为后缀访问                    
                    if ( $invalid_referer ) {      
                    #判断是否被盗链     
                        rewrite ^/ http://www.accp.com/error.png;
                        #判定被盗链就跳转错误图片
                }
}

[root@localhost named]# cp /abc/error.png /usr/local/nginx/html/
[root@localhost named]# systemctl stop nginx
[root@localhost named]# systemctl start nginx

第五步:在再此访问盗链网页,验证防盗效果

Nginx优化--网页压缩与配置防盗链

谢谢阅读!!!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI