Nginx的优化

隐藏版本号

原因:
每个版本都会有一些固有bug,为了防止部分人钻bug的漏洞破环或查看相关内容,所以要隐藏。
开启Nginx服务后就可以查询到当前版本
在这里插入图片描述
方法1:修改配置文件即可隐藏版本号
vim /usr/local/nginx/conf/nginx.conf
在这里插入图片描述
重启后

在这里插入图片描述

方法二:修改源码

vim /opt/nginx-1.15.9/src/core/nginx.h ##src就是放源码的位置
在这里插入图片描述

// 
cd  /opt/nginx-1.15.9

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 

 make && make install

在这里插入图片描述
重启后查看
在这里插入图片描述

配置网页缓存时间

原因:
为加快访问速度,对静态网页设置缓存时间

// 
vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

以图片作为缓存时间

// 
[root@localhost ~]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  bbs  index.html  index.php
[root@localhost html]# rz -E
rz waiting to receive.
[root@localhost html]# ls
50x.html  bbs  index.html  index.php  生生不息.jpg

在这里插入图片描述

修改用户和组

编译时指定用户组

// 
 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
 ststemctl restart nginx

在这里插入图片描述
编译时未指定用户与组

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

在这里插入图片描述
重启nginx
在这里插入图片描述

日志切割

原因
太大的日志文件不利于分析排查,因此需要定期进行日志文件的切割

// 
[root@localhost opt]# vim fenge.sh

在这里插入图片描述

执行脚本,查看结果

// 
[root@localhost ~]# ls /var/log/nginx/
test.con-access.log-20210629
[root@localhost ~]# date -s 20210628
20210628日 星期一 00:00:00 CST
[root@localhost ~]# /opt/fenge.sh
-bash: /opt/fenge.sh: 权限不够
[root@localhost ~]# chmod +x /opt/fenge.sh
[root@localhost ~]# /opt/fenge.sh
[root@localhost ~]# ls /var/log/nginx/
test.con-access.log-20210627  test.con-access.log-20210629
[root@localhost ~]# crontab -e  ##设置定时任务

在这里插入图片描述

设置连接超时

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

在这里插入图片描述
在这里插入图片描述

若将它设置为 0,就禁止了 keepalive 连接
在这里插入图片描述

Nginx的深入优化

更改进程数

高并发时可以修改为cpu核数的两倍

// 
[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical" 
8   ##cpu核数为8

在这里插入图片描述

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

在这里插入图片描述

// 
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ps aux | grep nginx

8个子进程
在这里插入图片描述

压缩网页

原因:
为节约网站的带宽,将网页内容在输出时进行压缩

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

在这里插入图片描述

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

在这里插入图片描述

// 
[root@localhost html]# systemctl restart nginx

访问网页192.168.142.144
在这里插入图片描述

防盗链

首先配置盗链

// 
源主机:192.168.142.145
盗链端:192.168.142.133
测试端:win10  192.168.142.128

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

盗链端修改配置文件

先查看原图路径
在这里插入图片描述

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

在这里插入图片描述
输入盗链端IP,查看
在这里插入图片描述

防盗链端配置

// 
[root@localhost html]# rz -E
rz waiting to receive.
[root@localhost html]# ls
123.jpg                              bbs              图片.jpg
50x.html                             index.html  
a9f5d8175fbec4e2500f5a619626f01.jpg  index.php   生生不息.jpg
[root@localhost html]# mv a9f5d8175fbec4e2500f5a619626f01.jpg error.png
[root@localhost html]# vim index.html

在这里插入图片描述

修改源主机配置文件为
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值