隐藏版本号
原因:
每个版本都会有一些固有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
2021年 06月 28日 星期一 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
修改源主机配置文件为