Nginx優(yōu)化---隱藏版本號與網(wǎng)頁緩存時間
配置Nginx隱藏版本號
在生產(chǎn)環(huán)境中,需要隱藏Nginx的版本號,以避免安全
漏洞的泄漏
查看方法
●使用fiddler I具在Windows客戶端查看Nginx版本號
在CentOS系統(tǒng)中使用“curl -I 網(wǎng)址”命令查看
Nginx隱藏版本號的方法
●修改配置文件法
●修改源碼法
修改配置文件法
1.Nginx的配置文件中的server_ tokens 選項的值設(shè)置為off
[root@www conf]# vim nginx.conf
.....
server_ tokens off;
.....
[root@www conf]# nginx -t
2.重啟服務(wù),訪問網(wǎng)站使用curl -I命令檢測
[root@www conf]# service nginx restart
[root@www conf]# curl -1 http://192.1 68.9.209/
HTTP/1.1200 OK
Server: nginx
3.若php配置文件中配置了fastcgi param SERVER SOFTWARE選項。則編輯php-fpm配置文件,將fastcgi param SERVER SOFTWARE對應(yīng)的值修改為
fastcgi_ param SERVER_ SOFTWARE nginx ;
修改源碼法
Nginx源碼文件/usr/src/nginx-1.12.0/src/core/nginx.h包含了版本信息,可以隨意設(shè)置重新編譯安裝,隱藏版本信息
示例:
#define NGINX_ _VERSION“1.1.1” ,修改版本號為1.1.1
#define NGINX_ VER "IIS/" ,修改軟件類型為IIS
重啟服務(wù),訪問網(wǎng)站使用curl -I命令檢測
修改Nginx用戶與組
Nginx運行時進程需要有用戶與組的支持,以實現(xiàn)對網(wǎng)站文件讀取時進行訪問控制
Nginx默認使用nobody用戶賬號與組賬號,一般也要進行修改
修改的方法
●編譯安裝時指定用戶與組
●修改配置文件指定用戶與組
修改配置文件法指定
1.新建用戶賬號,如nginx
2.修改主配置文件user選項,指定用戶賬號
3.重啟nginx服務(wù),使配置生效
4.使用ps aux命令查看nginx的進程信息,驗證運行用戶
賬號改變效果
[root@www conf]# vi nginx.conf
user nginx nginx;
[root@www conf]# service nginx restart
[root@www conf]# ps aux lgrep nginx
root 1300340.0 0.0 20220 620? Ss 19:41 0:00 nginx: master process
/usr/local/sbin/nginx
nginx 1300350.0 0.0 20664 1512 ?S 19:41 0:00 nginx: worker process
配置Nginx網(wǎng)頁緩存時間
當Nginx將網(wǎng)頁數(shù)據(jù)返回給客戶端后,可設(shè)置緩存的時間,以方便在日后進行相同內(nèi)容的請求時直接返回,避免重復(fù)請求,加快了訪問速度般針對靜態(tài)網(wǎng)頁設(shè)置,對動態(tài)網(wǎng)頁不設(shè)置緩存時間,可在Windows客戶端中使用fiddler查看網(wǎng)頁緩存時間
設(shè)置方法
可修改配置文件,在http段、 或者server段、 或者location段加入對特定內(nèi)容的過期參數(shù)
示例
修改Nginx的配置文件,在location段加入expires參數(shù)
location ~ \.(gifjpgliepglpnglbmplico)$ {
root html;
expires 1d;
隱藏版本號實例演示
一、編譯安裝Nginx服務(wù)
第一步:遠程獲取Windows上的源碼包,并掛載到Linux上
[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password:
Sharename Type Comment
--------- ---- -------
LNMP Disk
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:
[root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
game.jpg php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz
nginx-1.12.0.tar.gz
第二步:解壓源碼包
[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0 rh
第三步:下載安裝編譯組件包
[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \ //C語言
> gcc-c++ \ //c++語言
> pcre-devel \ //pcre語言工具
> zlib-devel //壓縮函數(shù)庫
第四步:創(chuàng)建程序用戶并配置Nginx服務(wù)相關(guān)組件
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//創(chuàng)建程序用戶nginx,并限定其不可登錄終端
[root@localhost opt]# cd nginx-1.12.0/
[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)計模塊
第五步:編譯與安裝Nginx
[root@localhost nginx-1.12.0]# make && make install
第六步:優(yōu)化Nginx服務(wù)啟動腳本,并建立命令軟連接
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
//創(chuàng)建nginx服務(wù)命令軟鏈接到系統(tǒng)命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service
//關(guān)閉防火墻
[root@localhost nginx-1.12.0]# setenforce 0
//關(guān)閉增強型安全功能
[root@localhost nginx-1.12.0]# nginx
//輸入nginx 開啟服務(wù)
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80 //查看服務(wù)的80 端口,顯示已開啟
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7520/nginx: master
第七步:systemctl管理nginx腳本
[root@localhost ~]# vim /lib/systemd/system/nginx.service ##創(chuàng)建配置文件
[Unit]
Description=nginx ##描述
After=network.target ##描述服務(wù)類型
[Service]
Type=forking ##后臺運行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ##啟動服務(wù)
ExecReload=/usr/bin/kill -s HUP $MAINPID ##根據(jù)PID重載配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ##根據(jù)PID終止進程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service ##設(shè)置執(zhí)行權(quán)限
[root@localhost ~]# systemctl stop nginx.service ##關(guān)閉nginx
[root@localhost ~]# systemctl start nginx.service ##開啟nginx
二、修改配置文件法隱藏版本號
第一步:默認情況下查看Nginx版本號
[root@localhost ~]# curl -I http://192.168.235.158 ##查看版本號
HTTP/1.1 200 OK
Server: nginx/1.12.0
##可見版本號為1.12.0
Date: Wed, 13 Nov 2019 08:32:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 06 Nov 2019 01:53:19 GMT
Connection: keep-alive
ETag: "5dc2278f-264"
Accept-Ranges: bytes
第二步:修改nginx.conf配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
##在http協(xié)議段落中加入server_ tokens選項的值設(shè)置為off即可
jpg
第三步:驗證Nginx版本號隱藏
[root@localhost ~]# systemctl stop nginx.service
[root@localhost ~]# systemctl start nginx.service
[root@localhost ~]# curl -I http://192.168.235.158
HTTP/1.1 200 OK
Server: nginx
##可見版本號已被隱藏
Date: Wed, 13 Nov 2019 09:18:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 06 Nov 2019 01:53:19 GMT
Connection: keep-alive
ETag: "5dc2278f-264"
Accept-Ranges: bytes
三、修改配置源碼法法隱藏版本號
第一步:修改nginx.conf配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
server_tokens on;
##將off替換成on
第二步:修改源碼文件nginx.h中的版本信息
[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h
#define NGINX_VERSION "1.1.1"
##更改版本信息為1.1.1
第三步:重新編譯Nginx
[root@localhost ~]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install
第四步:驗證Nginx版本號隱藏
[root@localhost nginx-1.12.0]# curl -I http://192.168.235.158
HTTP/1.1 200 OK
Server: nginx/1.1.1
##可見版本號已成功更改為1.1.1
Date: Wed, 13 Nov 2019 10:20:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 06 Nov 2019 01:53:19 GMT
Connection: keep-alive
ETag: "5dc2278f-264"
Accept-Ranges: bytes
網(wǎng)頁緩存時間實例演示
第一步:復(fù)制圖片到站點目錄
[root@localhost nginx-1.12.0]# ls /abc
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
game.jpg php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz
nginx-1.12.0.tar.gz
[root@localhost nginx-1.12.0]# cp /abc/game.jpg /usr/local/nginx/html/
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html game.jpg index.html
第二步:修改Nginx的index.html網(wǎng)頁
[root@localhost html]# vim index.html
<h1>Welcome to nginx!</h1>
<img src="game.jpg"/>
##在h1標簽下添加圖片路徑
第三步:修改Nginx .conf文件
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
##單獨輸入此行條目,指定用戶nginx,指定組nginx
location ~\.(gif|jepg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
##上述圖片類型圖片緩存一天
}
[root@localhost html]# systemctl stop nginx.service
[root@localhost html]# systemctl start nginx.service
第四步:打開一臺Win10虛擬機驗證
在客戶機中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網(wǎng)頁

總結(jié)
以上所述是小編給大家介紹的Nginx隱藏版本號與網(wǎng)頁緩存時間,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!