Nginx中gzip的应用
在配置文件中添加gzip模块
- 我们可以在官网中查看应用示例。
- 修改配置文件,修改完成后进行语法检测,并重启服务。
[root@server1 conf]# vim /usr/local/nginx/conf/nginx.conf #添加的内容如下所示:
33 gzip on;
34 gzip_min_length 1; #压缩条件:设置允许压缩的最小字节数,即大于1k就对其进行压缩
35 gzip_comp_level 3; # 压缩比:为1-9,1压缩比最小处理速度最快,9压缩比最大处理最慢(传输快但是比较消耗cpu)
36 gzip_types text/plain application/x-javascript text/css application /xml text/javascript application/x-httpd/php image/jpeg image/gif image/ png;
[root@server1 conf]# /usr/local/nginx/sbin/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@server1 conf]# systemctl reload nginx #重启
测试
-
(1)在默认发布页面导入较大的文件。
-
(2)在浏览器页面查看,发现我们的文件被压缩了(原始数据:传输的数据的大小transfer)
-
查看执行过程发现使用了gzip
对客户端的并发量和下载速度的限制
对客户端并发量的限制
- nginx可以使用
ngx_http_limit_req_module
模块的limit_req_zone
指令进行限流访问,防止用户恶意攻击刷爆服务器,ngx_http_limit_req_module
模块是nginix默认安装的,所以可以直接配置即可。
可以浏览官网查看使用帮助:
配置文件中添加限流限制
- 编辑配置文件添加相应的限制内容
配置文件编辑解析如下:在http的默认模块添加,区域名称zone为addr(自定义),占用空间大小为10M
- 编辑文件保存退出查看语法错误并且重启服务。
[root@server1 html]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 html]# /usr/local/nginx/sbin/nginx -t
[root@server1 html]# systemctl restart nginx.service
- 验证:在nginx的默认发布目录下建立所添加的资源/download/在里面放入图片
图片的大小为:444K
[root@server1 download]# du -sh vim.jpg
444K vim.jpg
- 在浏览器上查看所添加的资源是否准确
- 在物理机上再次进行压力测试查看实验效果。
[root@foundation24 pub]# ab -c 1 -n 10 http://172.25.24.1/download/vim.jpg #1个并发 10次请求。自带的压缩工具。测试访问速度和访问压力
burst,nodelay的添加
burst概念解析:
a.burst指缓冲队列的长度
b.nodelay字面的意思是不延迟,具体是说对用户发起的请求不做延迟处理,而是立即处理。比如上面所定义的rate=1r/s,即每秒处理一个请求。如果同一时刻有俩个后缀为.html的请求过来了,若设置了nodelay,则会立刻处理这俩个请求。若没设置nodelay,则会严格执行rate=1r/s的配置,即只处理一个请求,直观的看就是页面的数据卡了,过了1秒后才加载出来。
c.真正对限流起作用的配置就是rate=1r/s和burst=5这两个配置。
配置文件的修改如下:
框中第一行的含义解释:
a.在http的默认模块添加,区域名称zone为one(自定义),占用空间大小为10m,平均处理请求的频率不能超过每秒一次。
b.而$binary_remote_addr是remote_addr(客户端IP)的二进制格式,固定占用4个字节(可能是c语言的long类型长度)。而remote_addr是按照字符串存储,占用7−15个字节,这样看来remote_addr是按照字符串存储,占用7−15个字节,可以节省空间。
- 测试有无语法错误,重启服务。
[root@server1 download]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 download]# /usr/local/nginx/sbin/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@server1 download]# systemctl restart nginx.service
- 在物理机上再次进行压力测试查看实验效果(可以看到 处理一次并发,10次请求,一个域每次处理5个请求,每个请求花费1s,(排队)花费时间将近10s左右。)
对客户端的限流限制(limit_rate模块)
- 配置文件的修改,修改完成后检测有无语法错误,重启服务。(指的是对download这个资源页面进行限制)
[root@server1 download]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 download]# /usr/local/nginx/sbin/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@server1 download]# systemctl restart nginx.service
- 在物理机上进行压力测试查看实验效果。1次并发,10次请求,图片大小约440k,限速50k/s,所以每次请求用时约8s,10次请求用时80s。
- 在浏览器中访问查看效果:
- set $limit_rate 1k,显示nginx每秒传输一个字节到浏览器中
在nginx配置文件中的默认的域中添加该限流,添加完成后检测有没有语法错误,并重启服务。
[root@server1 download]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 download]# /usr/local/nginx/sbin/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@server1 download]# systemctl restart nginx.service
- 在浏览器中访问查看效果
指定采集访问日志的文件
- 先建立出要把日志采集到的文件,然后修改配置文件为如下所示(取消两处注释)。
[root@server1 download]# cd /usr/local/nginx/logs/
[root@server1 logs]# touch even.access.log
[root@server1 logs]# vim /usr/local/nginx/conf/nginx.conf
- 检测有无语法错误,重启服务,查看日志内容:
[root@server1 logs]# /usr/local/nginx/sbin/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@server1 logs]# systemctl restart nginx.service
[root@server1 logs]# ls
access.log backup.sh error.log even.access.log nginx.pid oldlogs
[root@server1 logs]# vim even.access.log
- 采集到的日志如下所示:
图片压缩
image模块的使用
模块的编译安装和导入
[root@server1 nginx-1.17.1]# /usr/local/nginx/sbin/nginx -V #在编译之前确定自己现在的环境和添加的模块
[root@server1 nginx-1.17.1]# ./configure --help | grep image #搜索image有关的模块
[root@server1 nginx-1.17.1]# ./configure --prefix=/usr/local/nginx --with-file-aio --with-http_realip_module --with-http_image_filter_module=dynamic #执行完这条命令会报错,因为缺少编译环境。
- 下载并安装
gd-devel-2.0.35-26.el7.x86_64.rpm
,重新编译,并安装。
lftp 172.25.24.24:/pub> get gd-devel-2.0.35-26.el7.x86_64.rpm
[root@server1 nginx-1.17.1]# yum install gd-devel-2.0.35-26.el7.x86_64.rpm -y
[root@server1 nginx-1.17.1]# ./configure --prefix=/usr/local/nginx --with-file-aio --with-http_realip_module --with-http_image_filter_module=dynamic #再次编译
[root@server1 nginx-1.17.1]# make #生成二进制文件
[root@server1 nginx-1.17.1]# cd objs/ #进入此目录查看安装的image模块
[root@server1 objs]# cp -f nginx /usr/local/nginx/sbin/nginx #覆盖旧的二进制文件
[root@server1 objs]# mkdir /usr/local/nginx/modules #因为image不是内置的模块,所以要导入使用,创建一个存放这个模块的目录
[root@server1 objs]# cp ngx_http_image_filter_module.so /usr/local/nginx/modules/ #把此模块复制到新建的目录中。
[root@server1 ~]# systemctl restart nginx.service
模块的使用
- 修改配置文件将模块导入。
[root@server1 download]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 download]# systemctl restart nginx.service
- 修改配置文件之前图片的大小。
- 修改像素之后的大小。
Nginx的正常访问
- 服务端配置:
[root@server1 conf]# vim nginx.conf #修改配置文件
129 server {
130 listen 80; #监听80端口
131 server_name www.even.com; #当在客户端访问www.even.com时,实际上访问的是/web/index.html
132
133 location / {
134 root /web;
135 index index.html;
136 }
137
138 }
[root@server1 conf]# mkdir /web
[root@server1 conf]# vim /web/index.html
[root@server1 conf]# cat /web/index.html
https'test page
[root@server1 conf]# systemctl restart nginx.service
- 在客户端做好解析测试:
https加密
- 1.关闭nginx服务,将模块编译安装和导入。
a.关掉nginx
[root@server1 download]# systemctl stop nginx.service
[root@server1 download]# systemctl status nginx.service
b.将模块编译安装和导入。
[root@server1 nginx-1.17.1]# /usr/local/nginx/sbin/nginx -V #首先查看已经编译好的模块
[root@server1 download]# cd ~/nginx-1.17.1/
[root@server1 nginx-1.17.1]# ./configure --help | grep ssl #查找https模块
[root@server1 nginx-1.17.1]# ./configure --prefix=/usr/local/nginx --with-file-aio --with-http_realip_module --with-http_image_filter_module=dynamic --with-http_ssl_module #编译安装
[root@server1 nginx-1.17.1]# make #生成二进制文件
[root@server1 nginx-1.17.1]# cd objs/
[root@server1 objs]# cp -f nginx /usr/local/nginx/sbin/nginx #覆盖旧的二进制文件
[root@server1 objs]# systemctl restart nginx.service #重启服务
[root@server1 objs]# cp ngx_http_image_filter_module.so /usr/local/nginx/modules/ #把新模块导入
[root@server1 objs]# systemctl restart nginx.service
[root@server1 objs]# /usr/local/nginx/sbin/nginx -V #查看是否编译好
- 生成证书,修改配置文件。
[root@server1 ~]# cd /etc/pki/tls/certs/
[root@server1 certs]# make cert.pem
[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/ #证书只有在此目录下才生效。
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf
111 server {
112 listen 443 ssl; #监听端口
113 server_name www.westos.com;
114
115 ssl_certificate cert.pem; #证书位置
116 ssl_certificate_key cert.pem; #私钥位置
117
118 ssl_session_cache shared:SSL:1m;
119 ssl_session_timeout 5m;
120
121 ssl_ciphers HIGH:!aNULL:!MD5;
122 ssl_prefer_server_ciphers on;
123
124 location / {
125 root /web;
126 index index.html index.htm;
127 }
128 }
[root@server1 conf]# mkdir /web
[root@server1 conf]# vim /web/index.html
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# cat /web/index.html
https'test page
[root@server1 conf]# systemctl restart nginx.service
-
443端口开启
-
测试,真机做解析,检测
-
在浏览器中输入
https://www.westos.com/
时,会出现导入证书界面。
网页重定向
临时重定向
将www.westos.com
重定向到https://www.westos.com
- 编写配置文件
- 重启服务
- 真机做好解析,测试:
注意:当在配置文件的rewrite重写后不加$时,效果如下: - 配置文件中去掉$符号
- 在真机测试,发现访问该域名下的haha.jpg文件时不能定向到
https://www.westos.com/haha.jpg
永久重定向
- 在配置文件rewrite行后面加permanent就可以。
- 真机测试
将www.westos.com/bbs/index.html
重定向到https://bbs.westos.com/index.html
- 编辑配置文件
- 编写测试页,重启服务。
[root@server1 conf]# mkdir /bbs
[root@server1 conf]# cd /bbs/
[root@server1 bbs]# vim index.html
[root@server1 bbs]# cat index.html
bbs.westos.com
- 在真机做解析,并测试
将bbs.westos.com
重定向到https://www.westos.com/bbs
下
- 编写配置文件,重启服务
- 在真机测试: