Nginx服务优化(由浅到深)!

Nginx服务优化

Nginx隐藏版本号

生产环境中为避免攻击者针对Nginx版本来进行攻击,需要隐藏版本号

查看版本号方式:

  • (1)查看头部信息
[root@localhost ~]# curl -I 192.168.43.150
HTTP/1.1 200 OK
Server: nginx/1.15.9                   #这里可以看到版本号
Date: Wed, 23 Jun 2021 10:35:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Jun 2021 09:44:24 GMT
Connection: keep-alive
ETag: "60d1b0f8-264"
Accept-Ranges: bytes
  • (2)在谷歌浏览器按F12键
    在这里插入图片描述

隐藏版本号的方式:

  • (1)通过修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# curl -I 192.168.43.150
HTTP/1.1 200 OK
Server: nginx                  #可以看到版本号已隐藏
Date: Wed, 23 Jun 2021 10:48:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Jun 2021 09:44:24 GMT
Connection: keep-alive
ETag: "60d1b0f8-264"
Accept-Ranges: bytes
  • (2)通过修改Nginx源码文件
[root@localhost ~]# vim /opt/nginx-1.15.9/src/core/nginx.h         #修改源码文件

在这里插入图片描述
重新编译安装Nginx

[root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --usr=nginx --group=nginx --with-http_stub_status
[root@localhost nginx-1.15.9]# make && make install
[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf    
[root@localhost nginx-1.15.9]# systemctl stop nginx.service 
[root@localhost nginx-1.15.9]# systemctl start nginx.service    

在这里插入图片描述

[root@localhost nginx-1.15.9]# curl -I 192.168.43.150
HTTP/1.1 200 OK
Server: apache/1.1.1                 #可以看到版本已隐藏
Date: Wed, 23 Jun 2021 11:05:17 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Jun 2021 09:44:24 GMT
Connection: keep-alive
ETag: "60d1b0f8-264"
Accept-Ranges: bytes

修改用户与组

在安装前没有创建用户时,则在此服务中默认使用的事nobody

[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf    #修改配置文件

在这里插入图片描述
验证结果
在这里插入图片描述

配置网页缓存时间

设置网页缓存时间,方便用户访问,一般只针对静态页面进行设置

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

在这里插入图片描述

[root@localhost html]# systemctl restart nginx.service 
[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html/        #进入放置网页目录下上传一张图片
[root@localhost html]# ls
50x.html
bbs
index.html
index.php
Snipaste_2021-06-23_17-30-02.png
[root@localhost html]# vim index.html 

在这里插入图片描述

[root@localhost html]# systemctl restart nginx.service     #重启服务

进入浏览器查看
在这里插入图片描述
也可以用curl命令查看

[root@localhost html]# curl -I 192.168.43.150/Snipaste_2021-06-23_17-30-02.png
HTTP/1.1 200 OK
Server: apache/1.1.1
Date: Wed, 23 Jun 2021 11:47:08 GMT
Content-Type: image/png
Content-Length: 381959
Last-Modified: Wed, 23 Jun 2021 09:30:10 GMT
Connection: keep-alive
ETag: "60d2ff22-5d407"
Expires: Thu, 24 Jun 2021 11:47:08 GMT
Cache-Control: max-age=86400                #这里可以看到缓存时间
Accept-Ranges: bytes

日志切割

随着Nginx运行时间的增加,其日志的大小也随之增加,为了防止日志太大不利于管理员的监控,需要通过Nginx信号控制功能的脚本时间日志的自动切割

用shell脚本编写实现日志切割

[root@localhost ~]# vim fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day""+%Y%m%d")                  #显示一天前的时间
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
kill -USR1 $(cat $pid_path)                      #重载nginx生成新的access日志
find $logs_path -mtime +30 | xargs rm -rf        #删除30天前的日志
[root@localhost ~]# chmod +x fenge.sh
[root@localhost ~]# sh -x fenge.sh               #测试脚本
++ date -d '-1 day' +%Y%m%d
+ d=20210623
+ logs_path=/var/log/nginx
+ pid_path=/usr/local/nginx/logs/nginx.pid
+ '[' -d /var/log/nginx ']'
+ mv /usr/local/nginx/logs/access.log /var/log/nginx/test.com-access.log-20210623
++ cat /usr/local/nginx/logs/nginx.pid
+ kill -USR1 23863
+ find /var/log/nginx -mtime +30
+ xargs rm -rf
[root@localhost ~]# ./fenge.sh 
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20210623
[root@localhost ~]# date -s 20210625             #将系统时间调为明天
2021年 06月 25日 星期五 00:00:00 CST
[root@localhost ~]# ./fenge.sh                   #再执行脚本
[root@localhost ~]# ls /var/log/nginx/           #查看日志
test.com-access.log-20210623  test.com-access.log-20210624
[root@localhost ~]# crontab -e                   
0 0-3 * * * /root/fenge.sh						#设置定时计划,每天0点至3点执行脚本

设置连接超时

为了避免客户长时间占用连接,造成资源浪费,可设置连接超时

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

在这里插入图片描述

Nginx优化深入

更改进程数

在高并发时,可开启更多Nginx进程用以快速响应用户的请求

[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical"     #查看cpu核数
8
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# ps aux | grep nginx
root      23863  0.0  0.0  20560   840 ?        Ss   624   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     23864  0.0  0.0  23016  1664 ?        S    624   0:00 nginx: worker process
root      25673  0.0  0.0 112724   988 pts/1    S+   00:24   0:00 grep --color=auto nginx

在这里插入图片描述

[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# ps aux | grep nginx              查看进程
root      25835  0.0  0.0  20560   652 ?        Ss   00:34   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     25836  0.0  0.0  23016  1396 ?        S    00:34   0:00 nginx: worker process
nginx     25837  0.0  0.0  23016  1420 ?        S    00:34   0:00 nginx: worker process
root      25847  0.0  0.0 112724   988 pts/1    S+   00:34   0:00 grep --color=auto nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf    #修改配置文件开启四个进程

在这里插入图片描述

[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# ps aux | grep nginx
root      25885  0.0  0.0  20560   652 ?        Ss   00:37   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     25886  0.0  0.0  23016  1420 ?        S    00:37   0:00 nginx: worker process
nginx     25887  0.0  0.0  23016  1416 ?        S    00:37   0:00 nginx: worker process
nginx     25888  0.0  0.0  23016  1420 ?        S    00:37   0:00 nginx: worker process
nginx     25889  0.0  0.0  23016  1420 ?        S    00:37   0:00 nginx: worker process
root      25891  0.0  0.0 112724   988 pts/1    S+   00:37   0:00 grep --color=auto nginx

配置网页压缩

Nginx服务有自带压缩模板,允许Nginx服务器将输出内容发送到客户端之前进行压缩,以节约网站的宽带,提升用户访问体验

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf    #修改配置文件

在这里插入图片描述

配置防盗链

在企业网站服务中,要设置防盗链功能来避免网站内容被非法盗用,造成经济损失

设备准备:一台CentOS7作为盗链端安装nginx服务,一台CentOS7作为服务端安装nginx服务,一台win10系统访问,关闭所有防火墙

(1)先配置盗链

服务端:

[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.150  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::fee0:5929:f3f8:11bc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:08:13:3d  txqueuelen 1000  (Ethernet)
        RX packets 26755  bytes 3851298 (3.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40908  bytes 11019318 (10.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# vim /etc/hosts    #设置域名和ip映射关系       
192.168.43.150 www.bt.com
[root@localhost ~]# cd /usr/local/nginx/html/    #将图片放在服务端
[root@localhost html]# ls
50x.html  bbs  index.html  index.php  Snipaste_2021-06-23_17-30-02.png
        

盗链端:

[root@nginx ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.66  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::792f:d024:3150:cf1d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:39:c8:59  txqueuelen 1000  (Ethernet)
        RX packets 963339  bytes 1313244142 (1.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 470025  bytes 35664798 (34.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@nginx ~]# vim /etc/hosts    #设置域名和IP地址映射        
192.168.43.66 www.test.com

Win10添加映射并访问网页
在这里插入图片描述
第一次更改hosts文件需要对其提权
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
确认后再配置文件中添加域名IP地址映射关系,保存
在这里插入图片描述
访问域名测试
在这里插入图片描述
在这里插入图片描述

在盗链端配置盗链

[root@nginx ~]# vim /usr/local/nginx/html/index.html    

在这里插入图片描述
在win10上访问盗链端域名,右键可查看图片为盗用,注意,每次重新查看网页是记得清理缓存

在这里插入图片描述

(2)配置防盗链
[root@nginx ~]# cd /usr/local/nginx/html/     #将盗链错误图片放在网页目录下
[root@nginx html]# ls
50x.html  bbs  index.html  index.php  Snipaste_2021-06-23_17-30-02.png  Snipaste_2021-06-24_22-57-19.png#此为错误图片
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf    #修改配置文件

在这里插入图片描述

FPM参数优化

Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整,FPM进程有两种启动方式,static将产生固定数据的fpm进程,dynamic将以动态方式产生fpm进程

[root@nginx ~]# vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20                  #static模式下空闲进程数上限,大于下面的值
pm.start_servers=5					#动态方式下默认开启的进程数,在最小和最大之间						
pm.min_spare_servers=2				#动态方式下最少空闲进程数
pm.max_ppare_servers=8				#动态方式下最大空闲进程数

总结

  • Nginx 服务优化包括隐藏版本号、更改用户和组、配置网页缓存时间、日志切割、设置连接超时
  • Nginx服务深入优化包括更改进程数、配置网页压缩、配置防盗链和FPM参数优化
  • Nginx优化提高用户体验、提高服务端运行效率
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值