Nginx隐藏版本号的方法
Nginx隐藏版本号
在生产环境中,需要隐藏Nginx的版本号,以避免安全漏洞的泄露
查看方法
使用fiddler工具在Windows客户端查看Nginx版本号
在centos系统中使用“curl-I网址”命令查看
Nginx隐藏版本号的方法
修改配置文件法
修改源码法
一,安装Nginx
1,在Linux上使用远程共享获取文件并挂载到mnt目录下
[root@localhost~]#smbclient-L//192.168.100.3/##远程共享访问 EnterSAMBA\root'spassword: SharenameTypeComment -------------------- LNMP-C7Disk [root@localhost~]#mount.cifs//192.168.100.3/LNMP-C7/mnt##挂载到/mnt目录下
2,解压源码包到/opt下,并查看
[root@localhost~]#cd/mnt##切换到挂载点目录 [root@localhostmnt]#ls Discuz_X3.4_SC_UTF8.zipnginx-1.12.2.tar.gz mysql-boost-5.7.20.tar.gzphp-7.1.20.tar.gz [root@localhostmnt]#tarzxvfnginx-1.12.2.tar.gz-C/opt##解压Nginx源码包到/opt下 [root@localhostmnt]#cd/opt/##切换到解压的目录下 [root@localhostopt]#ls nginx-1.12.2rh
3,安装编译需要的环境组件包
[root@localhostopt]#yum-yinstall\ gcc\//c语言 gcc-c++\//c++语言 pcre-devel\//pcre语言工具 zlib-devel//数据压缩用的函式库
4,创建程序用户nginx并编译Nginx
[root@localhostopt]#useradd-M-s/sbin/nologinnginx##创建程序用户,安全不可登陆状态 [root@localhostopt]#idnginx uid=1001(nginx)gid=1001(nginx)组=1001(nginx) [root@localhostopt]#cdnginx-1.12.0/##切换到nginx目录下 [root@localhostnginx-1.12.0]#./configure\##配置nginx >--prefix=/usr/local/nginx\##安装路径 >--user=nginx\##用户名 >--group=nginx\##用户组 >--with-http_stub_status_module##状态统计模块
5,编译和安装
[root@localhostnginx-1.12.0]#make##编译 ... [root@localhostnginx-1.12.0]#makeinstall##安装 ... [root@localhostnginx]#ln-s/usr/local/nginx/sbin/nginx/usr/local/sbin/ ##创建软连接让系统识别nginx启动脚本
6,制作管理脚本,便于使用service管理使用
[root@localhostnginx]#cd/etc/init.d/##切换到启动配置文件目录 [root@localhostinit.d]#ls functionsnetconsolenetworkREADME [root@localhostinit.d]#vimnginx##编辑启动脚本文件 #!/bin/bash #chkconfig:-9920##注释信息 #description:NginxServiceControlScript PROG="/usr/local/nginx/sbin/nginx"##设置变量为nginx命令文件 PIDF="/usr/local/nginx/logs/nginx.pid"##设置变量PID文件进程号为5346 case"$1"in start) $PROG##开启服务 ;; stop) kill-sQUIT$(cat$PIDF)##关闭服务 ;; restart)##重启服务 $0stop $0start ;; reload)##重载服务 kill-sHUP$(cat$PIDF) ;; *)##错误输入提示 echo"Usage:$0{start|stop|restart|reload}" exit1 esac exit0 [root@localhostinit.d]#chmod+x/etc/init.d/nginx##给启动脚本执行权限 [root@localhostinit.d]#chkconfig--addnginx##添加到service管理器中 [root@localhostinit.d]#servicenginxstop##就可以使用service控制nginx [root@localhostinit.d]#servicenginxstart
二,隐藏版本号
[root@localhostinit.d]#curl-Ihttp://192.168.13.140/##查看Nginx信息 HTTP/1.1200OK Server:nginx/1.12.2##显示版本号 Date:Tue,12Nov201914:23:24GMT Content-Type:text/html Content-Length:612 Last-Modified:Tue,12Nov201913:46:35GMT Connection:keep-alive ETag:"5dcab7bb-264" Accept-Ranges:bytes [root@localhostinit.d]#vim/usr/local/nginx/conf/nginx.conf##修改配置文件 http{##在http下添加 includemime.types; default_typeapplication/octet-stream; server_tokensoff;##关闭版本号 [root@localhostinit.d]#servicenginxstop##关闭服务 [root@localhostinit.d]#servicenginxstart##开启服务 [root@localhostinit.d]#curl-Ihttp://192.168.13.140/##查看Nginx信息 HTTP/1.1200OK Server:nginx##版本号被隐藏 Date:Tue,12Nov201914:22:00GMT Content-Type:text/html Content-Length:612 Last-Modified:Tue,12Nov201913:46:35GMT Connection:keep-alive ETag:"5dcab7bb-264" Accept-Ranges:bytes
三,伪造版本号(需要重新编译安装,可以在编译安装之前操作)
1,开启版本号
[root@localhostinit.d]#vim/usr/local/nginx/conf/nginx.conf http{ includemime.types; default_typeapplication/octet-stream; server_tokenson;##开启版本号
2,修改Nginx源码包文件
[root@localhostinit.d]#cd/opt/nginx-1.12.2/src/core/##切换到src源码包目录 [root@localhostcore]#vimnginx.h##修改文件 #defineNGINX_VERSION"1.1.1"##此处版本号伪造成1.1.1
3,重新编译安装
[root@localhostcore]#cd/opt/nginx-1.12.2/##切换目录到Nginx下 [root@localhostnginx-1.12.2]#./configure\##重新配置 >--prefix=/usr/local/nginx\ >--user=nginx\ >--group=nginx\ >--with-http_stub_status_module [root@localhostnginx-1.12.0]#make##重新编译 ... [root@localhostnginx-1.12.0]#makeinstall##重新安装 ...
4,重启Nginx服务,查看版本信息
[root@localhostnginx-1.12.2]#servicenginxstop##关闭 [root@localhostnginx-1.12.2]#servicenginxstart##开启 [root@localhostnginx-1.12.2]#curl-Ihttp://192.168.13.140/##查看Nginx信息 HTTP/1.1200OK Server:nginx/1.1.1##此时的版本号就是伪造的版本号 Date:Tue,12Nov201914:34:02GMT Content-Type:text/html Content-Length:612 Last-Modified:Tue,12Nov201913:46:35GMT Connection:keep-alive ETag:"5dcab7bb-264" Accept-Ranges:bytes
Nginx网页缓存时间
- 当Nginx将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
- 一般针对静态网页设置,对动态网页不设置缓存时间
- 可在Windows客户端中使用fiddler查看网页缓存时间
设置方法
可修改配置文件,在http段,或者server段,或者location段加入对特定内容的过期参数
实验环境
一台Nginx服务器
一台测试机win10
一,将图片复制到Nginx的站点目录下
[root@localhost~]#cd/mnt/##切换到挂载点 [root@localhostmnt]#ls http://dh.wk163.com11.jpgmysql-boost-5.7.20.tar.gzphp-7.1.20.tar.gz 22.jpgnginx-1.12.2.tar.gz Discuz_X3.4_SC_UTF8.zipphp-7.1.10.tar.bz2 [root@localhostmnt]#cphttp://dh.wk163.com11.jpg/usr/local/nginx/html/##复制图片到站点中 [root@localhostmnt]#cd/usr/local/nginx/html/##切换到站点下 [root@localhosthtml]#ls http://dh.wk163.com11.jpg50x.htmlindex.html
二,修改网页信息,将图片加到index.html文件中
[root@localhosthtml]#vimindex.html##修改网页信息Welcometonginx!
##加入图片到网页中
三,修改配置文件信息
[root@localhosthtml]#vim/usr/local/nginx/conf/nginx.conf##修改配置文件 events{ worker_connections1024; } usernginxnginx;##修改Nginx用户和组 #denyaccessto.htaccessfiles,ifApache'sdocumentroot #concurswithnginx'sone # location~\.(gif|jepg|jpg|ico|bmp|png)${##支持图片格式 roothtml;##站点 expires1d;##缓存一天 } [root@localhosthtml]#servicenginxstop##关闭开启服务 [root@localhosthtml]#servicenginxstart
四,用fiddler查看缓存
Nginx的日志切割
随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注日志文件
太大的日志文件对监控是一个大灾难
定期进行日志文件的切割
Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割,并通过Linux的计划任务周期性的进行日志切割
1,编写日志分割脚本文件
[root@localhost~]#vimfenge.sh##编写脚本文件 #!/bin/bash #Filename:fenge.sh d=$(date-d"-1day""+%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|xargsrm-rf##删除30天前的日志文件 [root@localhost~]#chmod+xfenge.sh##给执行权限 [root@localhost~]#./fenge.sh##执行脚本文件
2,查看日志分割情况
[root@localhost~]#cd/var/log/nginx/##切换到Nginx的日志目录下 [root@localhostnginx]#ls test.com-access.log-20191112 [root@localhostnginx]#date-s2019-11-14##修改日期为明天的时间 2019年11月14日星期四00:00:00CST [root@localhostnginx]#cd~ [root@localhost~]#./fenge.sh##重新执行脚本 [root@localhost~]#cd/var/log/nginx/ [root@localhostnginx]#ls##查看日志分割日志文件 test.com-access.log-20191112test.com-access.log-20191113
3,设置周期性计划任务
[root@localhostnginx]#crontab-e##周期性计划任务 01***/opt/fenge.sh
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。