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網頁緩存時間

  • 當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>
<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運行狀態,需要時刻關注日誌文件
  • 太大的日誌文件對監控是一個大災難
    定期進行日誌文件的切割
  • 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

謝謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章