温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何隐藏Nginx的版本号

发布时间:2020-06-08 09:18:13 来源:亿速云 阅读:321 作者:Leah 栏目:云计算

如何隐藏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]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz
mysql-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]# ls
nginx-1.12.2  rh

3,安装编译需要的环境组件包

[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 nginx
uid=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]# ls
functions  netconsole  network  README
[root@localhost init.d]# vim nginx         ##编辑启动脚本文件

#!/bin/bash
# chkconfig: - 99 20                                    ##注释信息
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"           ##设置变量为nginx命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"       ##设置变量PID文件 进程号为5346
case "$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 1
esac
exit 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 OK
Server: nginx/1.12.2    ##显示版本号
Date: Tue, 12 Nov 2019 14:23:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT
Connection: keep-alive
ETag: "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 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes

三,伪造版本号(需要重新编译安装,可以在编译安装之前操作)

1,开启版本号

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
http {
        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.1

3,重新编译安装

[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 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes

Nginx网页缓存时间

  • 当Nginx将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
  • 一般针对静态网页设置,对动态网页不设置缓存时间
  • 可在Windows客户端中使用fiddler查看网页缓存时间

设置方法

可修改配置文件,在http段,或者server段,或者location段加入对特定内容的过期参数

实验环境

一台Nginx服务器
一台测试机win10

一,将图片复制到Nginx的站点目录下

[root@localhost ~]# cd /mnt/   ##切换到挂载点
[root@localhost mnt]# ls
11.jpg                   mysql-boost-5.7.20.tar.gz  php-7.1.20.tar.gz
22.jpg                   nginx-1.12.2.tar.gz
Discuz_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]# ls
11.jpg  50x.html  index.html

二,修改网页信息,将图片加到index.html文件中

[root@localhost html]# vim index.html  ##修改网页信息

</head>
<body>
<h2>Welcome to nginx!</h2>
<img src="11.jpg"/>  ##加入图片到网页中

三,修改配置文件信息

[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运行状态,需要时刻关注日志文件
  • 太大的日志文件对监控是一个大灾难
    定期进行日志文件的切割
  • Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割,并通过Linux的计划任务周期性的进行日志切割

1,编写日志分割脚本文件

[root@localhost ~]# vim fenge.sh  ##编写脚本文件

#!/bin/bash
#Filename:fenge.sh
d=$(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]# ls
test.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-20191113

3,设置周期性计划任务

[root@localhost nginx]# crontab -e   ##周期性计划任务
0 1 * * * /opt/fenge.sh

关于隐藏Nginx的版本号的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI