• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    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]# 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网页缓存时间

    设置方法

    可修改配置文件,在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>
    <h1>Welcome to nginx!</h1>
    <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信号控制功能的脚本实现日志的自动切割,并通过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
    

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    上一篇:CentOS7.5安装配置Harbor1.7的全过程
    下一篇:Linux 中有效用户组和初始用户组的实现
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    Nginx隐藏版本号的方法 Nginx,隐藏,版本,号的,方法,