nginx优化
1.隐藏版本号
第一种通用方式
只要是nginx就行
缺点:只能隐藏版本号,不能隐藏服务名称
修改nginx配置文件
在http模块中添加一串代码
server_tokens off;
配置完后记得重启
systemctl restart nginx.service
第二种方式(仅限编译安装)
可以随意修改服务名和版本号
修改源码包
/opt/nginx-1.22.0/src/core
配置完后需要重新编译安装
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make -j 4 && make install
修改nginx配置
重启
systemctl restart nginx.service
2.日志分割
(nginx本身没有日志分割)
使用shell脚本实现日志分割
#!/bin/bash
d=$(date +%Y-%m-%d)
#直接获取当前的日期
dir="/usr/local/nginx/logs"
#获取日志目录所在的位置
logs_access='/usr/local/nginx/logs/access.log'
logs_error='/usr/local/nginx/logs/error.log'
#定义需要分割的日志分件
pid_file='/usr/local/nginx/run/nginx.pid'
#定义pid分件的位置
if [ ! -d "$dir" ]
then
mkdir -p $dir
fi
mv ${logs_access} ${dir}/access_${d}.log
#mv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access_2024-12-09.log
mv ${logs_error} ${dir}/error_${d}.log
#mv /usr/local/nginx/logs/error.log /usr/local/nginx/logs/error_2024-12-09.log
kill -USR1 $(cat ${pid_file})
#-USR1,通过发送USR1这个信号,给nginx的主进程,让nginx重新生成acces.log和error.log
#日志清理. 清理30天以前的日志
find $dir -mtime +30 -exec rm -rf {} \;
注意后续日志还会写在源日志中
除非修改nginx配置文件
创建定时任务
crontab -e -u root
0 0 * * * /opt/nginxlog.sh
记得赋权执行
chmod 777 nginxlog.sh
3.页面压缩
网站图片的缓存
会话保持和连接保持
nginx408的原因
nginx并发处理和timewait的回收机制
如何回收timewait
改内核文件
vim /etc/sysctl.conf
配置防盗链
修改nginx配置文件
配置网页信息
vim index.html
<img src="neuro.gif"/>
</body>
</html>
做本地域名解析
vim /etc/hosts
给客户机也做同样的配置
vim index.html
<img src="http://www.xy104.com/neuro.gif"/>
</body>
</html>
vim /etc/hosts
192.168.32.61 www.xy104.com
192.168.32.62 www.xy105.com
记得启动nginx
systemctl restart nginx.service
nginx bug
访问xy104正常
无法显示xy105
删除nginx配置文件中的png即可
修改后可正常显示