如何隱藏Nginx的版本號(hào)-創(chuàng)新互聯(lián)

如何隱藏Nginx的版本號(hào)?針對(duì)這個(gè)問(wèn)題,今天小編總結(jié)這篇有關(guān)Nginx版本號(hào)的文章,希望能幫助更多想解決這個(gè)問(wèn)題的朋友找到更加簡(jiǎn)單易行的辦法。

主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、自適應(yīng)網(wǎng)站建設(shè)、程序開(kāi)發(fā)、微網(wǎng)站、小程序開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。

查看方法

使用fiddler工具在Windows客戶端查看Nginx版本號(hào)
在centos系統(tǒng)中使用“curl -I 網(wǎng)址” 命令查看

Nginx隱藏版本號(hào)的方法

修改配置文件法
修改源碼法

一,安裝Nginx

1,在Linux上使用遠(yuǎn)程共享獲取文件并掛載到mnt目錄下

[root@localhost ~]# smbclient -L //192.168.100.3/  ##遠(yuǎn)程共享訪問(wèn)
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   ##切換到掛載點(diǎn)目錄
[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,安裝編譯需要的環(huán)境組件包

[root@localhost opt]# yum -y install \
gcc \                    //c語(yǔ)言
gcc-c++ \             //c++語(yǔ)言
pcre-devel \           //pcre語(yǔ)言工具
zlib-devel            //數(shù)據(jù)壓縮用的函式庫(kù)

4,創(chuàng)建程序用戶nginx并編譯Nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx  ##創(chuàng)建程序用戶,安全不可登陸狀態(tài)
[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   ##狀態(tài)統(tǒng)計(jì)模塊

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/ 
##創(chuàng)建軟連接讓系統(tǒng)識(shí)別nginx啟動(dòng)腳本

6,制作管理腳本,便于使用service管理使用

[root@localhost nginx]# cd /etc/init.d/  ##切換到啟動(dòng)配置文件目錄
[root@localhost init.d]# ls
functions  netconsole  network  README
[root@localhost init.d]# vim nginx     ##編輯啟動(dòng)腳本文件

#!/bin/bash
# chkconfig: - 99 20                   ##注釋信息
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"      ##設(shè)置變量為nginx命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"    ##設(shè)置變量PID文件 進(jìn)程號(hào)為5346
case "$1" in  
         start)
                 $PROG                    ##開(kāi)啟服務(wù)
                 ;;
         stop)
                 kill -s QUIT $(cat $PIDF)       ##關(guān)閉服務(wù)
                 ;;
         restart)                        ##重啟服務(wù)
                 $0 stop
                 $0 start
                 ;;
         reload)                         ##重載服務(wù)
                 kill -s HUP $(cat $PIDF)
                 ;;
         *)                              ##錯(cuò)誤輸入提示
                 echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0
[root@localhost init.d]# chmod +x /etc/init.d/nginx   ##給啟動(dòng)腳本執(zhí)行權(quán)限
[root@localhost init.d]# chkconfig --add nginx      ##添加到service管理器中
[root@localhost init.d]# service nginx stop         ##就可以使用service控制nginx
[root@localhost init.d]# service nginx start

二,隱藏版本號(hào)

[root@localhost init.d]# curl -I http://192.168.13.140/  ##查看Nginx信息
HTTP/1.1 200 OK
Server: nginx/1.12.2   ##顯示版本號(hào)
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;   ##關(guān)閉版本號(hào)

[root@localhost init.d]# service nginx stop  ##關(guān)閉服務(wù)
[root@localhost init.d]# service nginx start  ##開(kāi)啟服務(wù)
[root@localhost init.d]# curl -I http://192.168.13.140/  ##查看Nginx信息
HTTP/1.1 200 OK    
Server: nginx       ##版本號(hào)被隱藏
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

三,偽造版本號(hào)(需要重新編譯安裝,可以在編譯安裝之前操作)

1,開(kāi)啟版本號(hào)

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
http {
     include    mime.types;
     default_type  application/octet-stream;
     server_tokens on;  ##開(kāi)啟版本號(hào)

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"  ##此處版本號(hào)偽造成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服務(wù),查看版本信息

[root@localhost nginx-1.12.2]# service nginx stop  ##關(guān)閉
[root@localhost nginx-1.12.2]# service nginx start  ##開(kāi)啟
[root@localhost nginx-1.12.2]# curl -I http://192.168.13.140/  ##查看Nginx信息
HTTP/1.1 200 OK 
Server: nginx/1.1.1    ##此時(shí)的版本號(hào)就是偽造的版本號(hào)
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網(wǎng)頁(yè)緩存時(shí)間

  • 當(dāng)Nginx將網(wǎng)頁(yè)數(shù)據(jù)返回給客戶端后,可設(shè)置緩存時(shí)間,以方便在日后進(jìn)行相同內(nèi)容的請(qǐng)求時(shí)直接返回,避免重復(fù)請(qǐng)求,加快了訪問(wèn)速度
  • 一般針對(duì)靜態(tài)網(wǎng)頁(yè)設(shè)置,對(duì)動(dòng)態(tài)網(wǎng)頁(yè)不設(shè)置緩存時(shí)間
  • 可在Windows客戶端中使用fiddler查看網(wǎng)頁(yè)緩存時(shí)間

設(shè)置方法

可修改配置文件,在http段,或者server段,或者location段加入對(duì)特定內(nèi)容的過(guò)期參數(shù)

實(shí)驗(yàn)環(huán)境

一臺(tái)Nginx服務(wù)器
一臺(tái)測(cè)試機(jī)win10

一,將圖片復(fù)制到Nginx的站點(diǎn)目錄下

[root@localhost ~]# cd /mnt/  ##切換到掛載點(diǎn)
[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/  ##復(fù)制圖片到站點(diǎn)中
[root@localhost mnt]# cd /usr/local/nginx/html/   ##切換到站點(diǎn)下
[root@localhost html]# ls
11.jpg  50x.html  index.html

二,修改網(wǎng)頁(yè)信息,將圖片加到index.html文件中

[root@localhost html]# vim index.html  ##修改網(wǎng)頁(yè)信息

</head>
<body>
<h2>Welcome to nginx!</h2>
<img src="11.jpg"/>  ##加入圖片到網(wǎng)頁(yè)中

三,修改配置文件信息

[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;   ##站點(diǎn)
     expires 1d;  ##緩存一天
     }
[root@localhost html]# service nginx stop  ##關(guān)閉開(kāi)啟服務(wù)
[root@localhost html]# service nginx start

四,用fiddler查看緩存

如何隱藏Nginx的版本號(hào)
如何隱藏Nginx的版本號(hào)

Nginx的日志切割

  • 隨著Nginx運(yùn)行時(shí)間增加,日志也會(huì)增加。為了方便掌握Nginx運(yùn)行狀態(tài),需要時(shí)刻關(guān)注日志文件
  • 太大的日志文件對(duì)監(jiān)控是一個(gè)大災(zāi)難
    定期進(jìn)行日志文件的切割
  • Nginx自身不具備日志分割處理的功能,但可以通過(guò)Nginx信號(hào)控制功能的腳本實(shí)現(xiàn)日志的自動(dòng)切割,并通過(guò)Linux的計(jì)劃任務(wù)周期性的進(jìn)行日志切割

1,編寫(xiě)日志分割腳本文件

[root@localhost ~]# vim fenge.sh  ##編寫(xiě)腳本文件

#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")     ##顯示一天前的時(shí)間
logs_path="/var/log/nginx"            ##分割日志的保存路徑
pid_path="/usr/local/nginx/logs/nginx.pid"   ##pid的路徑
[ -d $logs_path ] || mkdir -p $logs_path  ##沒(méi)有目錄則創(chuàng)建目錄
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
##原有日志文件生成到新路徑下
kill -USR1 $(cat $pid_path)  ##結(jié)束重新生成新的pid文件
find $logs_path -mtime +30 | xargs rm -rf  ##刪除30天前的日志文件

[root@localhost ~]# chmod +x fenge.sh  ##給執(zhí)行權(quán)限
[root@localhost ~]# ./fenge.sh   ##執(zhí)行腳本文件

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  ##修改日期為明天的時(shí)間
2019年 11月 14日 星期四 00:00:00 CST
[root@localhost nginx]# cd ~
[root@localhost ~]# ./fenge.sh   ##重新執(zhí)行腳本
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ls      ##查看日志分割日志文件
test.com-access.log-20191112  test.com-access.log-20191113

3,設(shè)置周期性計(jì)劃任務(wù)

[root@localhost nginx]# crontab -e  ##周期性計(jì)劃任務(wù)
0 1 * * * /opt/fenge.sh

關(guān)于隱藏Nginx的版本號(hào)的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

新聞標(biāo)題:如何隱藏Nginx的版本號(hào)-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article6/dposog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作網(wǎng)站排名、網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)