Nginx优化——隐藏版本号,网页缓存,日志分割(一)
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,Nginx隐藏版本号在生产环境中,需要隐藏Nginx的版本号,以避免安全漏洞的泄露查看方法使用fiddler工具在Windows客户端查看Nginx版本号在centos系统中使用"curl -I 网址
千家信息网最后更新 2025年12月02日Nginx优化——隐藏版本号,网页缓存,日志分割(一)
Nginx隐藏版本号
在生产环境中,需要隐藏Nginx的版本号,以避免安全漏洞的泄露查看方法
使用fiddler工具在Windows客户端查看Nginx版本号在centos系统中使用"curl -I 网址" 命令查看Nginx隐藏版本号的方法
修改配置文件法修改源码法一,安装Nginx
1,在Linux上使用远程共享获取文件并挂载到mnt目录下
[root@localhost ~]# smbclient -L //192.168.100.3/ ##远程共享访问Enter SAMBA\root's password: Sharename Type Comment --------- ---- ------- LNMP-C7 Disk [root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt ##挂载到/mnt目录下2,解压源码包到/opt下,并查看
[root@localhost ~]# cd /mnt ##切换到挂载点目录[root@localhost mnt]# lsDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gzmysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz[root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt ##解压Nginx源码包到/opt下[root@localhost mnt]# cd /opt/ ##切换到解压的目录下[root@localhost opt]# lsnginx-1.12.2 rh3,安装编译需要的环境组件包
[root@localhost opt]# yum -y install \gcc \ //c语言gcc-c++ \ //c++语言pcre-devel \ //pcre语言工具zlib-devel //数据压缩用的函式库4,创建程序用户nginx并编译Nginx
[root@localhost opt]# useradd -M -s /sbin/nologin nginx ##创建程序用户,安全不可登陆状态[root@localhost opt]# id nginxuid=1001(nginx) gid=1001(nginx) 组=1001(nginx)[root@localhost opt]# cd nginx-1.12.0/ ##切换到nginx目录下[root@localhost nginx-1.12.0]# ./configure \ ##配置nginx> --prefix=/usr/local/nginx \ ##安装路径> --user=nginx \ ##用户名> --group=nginx \ ##用户组> --with-http_stub_status_module ##状态统计模块5,编译和安装
[root@localhost nginx-1.12.0]# make ##编译...[root@localhost nginx-1.12.0]# make install ##安装...[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##创建软连接让系统识别nginx启动脚本6,制作管理脚本,便于使用service管理使用
[root@localhost nginx]# cd /etc/init.d/ ##切换到启动配置文件目录[root@localhost init.d]# lsfunctions netconsole network README[root@localhost init.d]# vim nginx ##编辑启动脚本文件#!/bin/bash# chkconfig: - 99 20 ##注释信息# description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx" ##设置变量为nginx命令文件PIDF="/usr/local/nginx/logs/nginx.pid" ##设置变量PID文件 进程号为5346case "$1" in start) $PROG ##开启服务 ;; stop) kill -s QUIT $(cat $PIDF) ##关闭服务 ;; restart) ##重启服务 $0 stop $0 start ;; reload) ##重载服务 kill -s HUP $(cat $PIDF) ;; *) ##错误输入提示 echo "Usage: $0 {start|stop|restart|reload}" exit 1esacexit 0[root@localhost init.d]# chmod +x /etc/init.d/nginx ##给启动脚本执行权限[root@localhost init.d]# chkconfig --add nginx ##添加到service管理器中[root@localhost init.d]# service nginx stop ##就可以使用service控制nginx[root@localhost init.d]# service nginx start二,隐藏版本号
[root@localhost init.d]# curl -I http://192.168.13.140/ ##查看Nginx信息HTTP/1.1 200 OKServer: nginx/1.12.2 ##显示版本号Date: Tue, 12 Nov 2019 14:23:24 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf ##修改配置文件http { ##在http下添加 include mime.types; default_type application/octet-stream; server_tokens off; ##关闭版本号[root@localhost init.d]# service nginx stop ##关闭服务[root@localhost init.d]# service nginx start ##开启服务[root@localhost init.d]# curl -I http://192.168.13.140/ ##查看Nginx信息HTTP/1.1 200 OK Server: nginx ##版本号被隐藏Date: Tue, 12 Nov 2019 14:22:00 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes三,伪造版本号(需要重新编译安装,可以在编译安装之前操作)
1,开启版本号
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.confhttp { include mime.types; default_type application/octet-stream; server_tokens on; ##开启版本号2,修改Nginx源码包文件
[root@localhost init.d]# cd /opt/nginx-1.12.2/src/core/ ##切换到src源码包目录[root@localhost core]# vim nginx.h ##修改文件#define NGINX_VERSION "1.1.1" ##此处版本号伪造成1.1.13,重新编译安装
[root@localhost core]# cd /opt/nginx-1.12.2/ ##切换目录到Nginx下[root@localhost nginx-1.12.2]# ./configure \ ##重新配置> --prefix=/usr/local/nginx \> --user=nginx \> --group=nginx \> --with-http_stub_status_module[root@localhost nginx-1.12.0]# make ##重新编译...[root@localhost nginx-1.12.0]# make install ##重新安装...4,重启Nginx服务,查看版本信息
[root@localhost nginx-1.12.2]# service nginx stop ##关闭[root@localhost nginx-1.12.2]# service nginx start ##开启[root@localhost nginx-1.12.2]# curl -I http://192.168.13.140/ ##查看Nginx信息HTTP/1.1 200 OK Server: nginx/1.1.1 ##此时的版本号就是伪造的版本号Date: Tue, 12 Nov 2019 14:34:02 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytesNginx网页缓存时间
- 当Nginx将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
- 一般针对静态网页设置,对动态网页不设置缓存时间
- 可在Windows客户端中使用fiddler查看网页缓存时间
设置方法
可修改配置文件,在http段,或者server段,或者location段加入对特定内容的过期参数实验环境
一台Nginx服务器一台测试机win10一,将图片复制到Nginx的站点目录下
[root@localhost ~]# cd /mnt/ ##切换到挂载点[root@localhost mnt]# ls11.jpg mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz22.jpg nginx-1.12.2.tar.gzDiscuz_X3.4_SC_UTF8.zip php-7.1.10.tar.bz2 [root@localhost mnt]# cp 11.jpg /usr/local/nginx/html/ ##复制图片到站点中[root@localhost mnt]# cd /usr/local/nginx/html/ ##切换到站点下[root@localhost html]# ls11.jpg 50x.html index.html二,修改网页信息,将图片加到index.html文件中
[root@localhost html]# vim index.html ##修改网页信息Welcome to nginx!
##加入图片到网页中三,修改配置文件信息
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf ##修改配置文件events { worker_connections 1024;} user nginx nginx; ##修改Nginx用户和组 # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~\.(gif|jepg|jpg|ico|bmp|png)$ { ##支持图片格式 root html; ##站点 expires 1d; ##缓存一天 }[root@localhost html]# service nginx stop ##关闭开启服务[root@localhost html]# service nginx start 四,用fiddler查看缓存

Nginx的日志切割
- 随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注日志文件
- 太大的日志文件对监控是一个大灾难
定期进行日志文件的切割- Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割,并通过Linux的计划任务周期性的进行日志切割
1,编写日志分割脚本文件
[root@localhost ~]# vim fenge.sh ##编写脚本文件#!/bin/bash#Filename:fenge.shd=$(date -d "-1 day" "+%Y%m%d") ##显示一天前的时间logs_path="/var/log/nginx" ##分割日志的保存路径pid_path="/usr/local/nginx/logs/nginx.pid" ##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) ##结束重新生成新的pid文件find $logs_path -mtime +30 | xargs rm -rf ##删除30天前的日志文件[root@localhost ~]# chmod +x fenge.sh ##给执行权限[root@localhost ~]# ./fenge.sh ##执行脚本文件2,查看日志分割情况
[root@localhost ~]# cd /var/log/nginx/ ##切换到Nginx的日志目录下[root@localhost nginx]# lstest.com-access.log-20191112[root@localhost nginx]# date -s 2019-11-14 ##修改日期为明天的时间2019年 11月 14日 星期四 00:00:00 CST[root@localhost nginx]# cd ~[root@localhost ~]# ./fenge.sh ##重新执行脚本[root@localhost ~]# cd /var/log/nginx/[root@localhost nginx]# ls ##查看日志分割日志文件test.com-access.log-20191112 test.com-access.log-201911133,设置周期性计划任务
[root@localhost nginx]# crontab -e ##周期性计划任务0 1 * * * /opt/fenge.sh谢谢阅读!
文件
日志
版本
目录
脚本
切换
服务
网页
信息
编译
配置
时间
缓存
图片
源码
用户
路径
任务
周期
周期性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
维特数据库
百度程序员删除数据库
农合数据库操作出错
工作软件开发
南京特亮网络技术服务有限公司
武汉大学网络安全学院薪
计算机网络技术求职专业
武汉系统软件开发服务费
如何监控软件开发项目进度表
上海勤和软件开发有限公司
网络安全政治稳定
公司网络服务器对孕妇有影响吗
加强互联网网络安全防护
自己搞一个原神服务器
用友t3装不了数据库
软件开发招投标周期
使用网络时如何做好网络安全防范
软件开发项目安全保障工作
全国网络安全法学习
兰州哪有卖二手服务器的市场
传奇数据库密码重置
服务器可以改装电脑吗
欧盟关于欧洲网络安全局
组态王数据库上传其他电脑
大二层网络安全防范
互联网科技做销售怎么样
科达服务器管理口ip
数据库范围查询效
丽江网络安全执法
什么是网络全文数据库