Nginx隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間的方法
Nginx優(yōu)化---隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間
配置Nginx隱藏版本號(hào)
在生產(chǎn)環(huán)境中,需要隱藏Nginx的版本號(hào),以避免安全
漏洞的泄漏
查看方法
●使用fiddler I具在Windows客戶端查看Nginx版本號(hào)
在CentOS系統(tǒng)中使用“curl -I 網(wǎng)址”命令查看
Nginx隱藏版本號(hào)的方法
●修改配置文件法
●修改源碼法
修改配置文件法
1.Nginx的配置文件中的server_ tokens 選項(xiàng)的值設(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選項(xiàng)。則編輯php-fpm配置文件,將fastcgi param SERVER SOFTWARE對(duì)應(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” ,修改版本號(hào)為1.1.1 #define NGINX_ VER "IIS/" ,修改軟件類型為IIS
重啟服務(wù),訪問網(wǎng)站使用curl -I命令檢測
修改Nginx用戶與組
Nginx運(yùn)行時(shí)進(jìn)程需要有用戶與組的支持,以實(shí)現(xiàn)對(duì)網(wǎng)站文件讀取時(shí)進(jìn)行訪問控制
Nginx默認(rèn)使用nobody用戶賬號(hào)與組賬號(hào),一般也要進(jìn)行修改
修改的方法
●編譯安裝時(shí)指定用戶與組
●修改配置文件指定用戶與組
修改配置文件法指定
1.新建用戶賬號(hào),如nginx
2.修改主配置文件user選項(xiàng),指定用戶賬號(hào)
3.重啟nginx服務(wù),使配置生效
4.使用ps aux命令查看nginx的進(jìn)程信息,驗(yàn)證運(yùn)行用戶
賬號(hào)改變效果
[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)頁緩存時(shí)間
當(dāng)Nginx將網(wǎng)頁數(shù)據(jù)返回給客戶端后,可設(shè)置緩存的時(shí)間,以方便在日后進(jìn)行相同內(nèi)容的請(qǐng)求時(shí)直接返回,避免重復(fù)請(qǐng)求,加快了訪問速度般針對(duì)靜態(tài)網(wǎng)頁設(shè)置,對(duì)動(dòng)態(tài)網(wǎng)頁不設(shè)置緩存時(shí)間,可在Windows客戶端中使用fiddler查看網(wǎng)頁緩存時(shí)間
設(shè)置方法
可修改配置文件,在http段、 或者server段、 或者location段加入對(duì)特定內(nèi)容的過期參數(shù)
示例
修改Nginx的配置文件,在location段加入expires參數(shù)
location ~ \.(gifjpgliepglpnglbmplico)$ { root html; expires 1d;
隱藏版本號(hào)實(shí)例演示
一、編譯安裝Nginx服務(wù)
第一步:遠(yuǎn)程獲取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)計(jì)模塊
第五步:編譯與安裝Nginx
[root@localhost nginx-1.12.0]# make && make install
第六步:優(yōu)化Nginx服務(wù)啟動(dòng)腳本,并建立命令軟連接
[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)閉增強(qiáng)型安全功能 [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 ##后臺(tái)運(yùn)行形式 PIDFile=/usr/local/nginx/logs/nginx.pid ##PID文件位置 ExecStart=/usr/local/nginx/sbin/nginx ##啟動(dòng)服務(wù) ExecReload=/usr/bin/kill -s HUP $MAINPID ##根據(jù)PID重載配置 ExecStop=/usr/bin/kill -s QUIT $MAINPID ##根據(jù)PID終止進(jìn)程 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
二、修改配置文件法隱藏版本號(hào)
第一步:默認(rèn)情況下查看Nginx版本號(hào)
[root@localhost ~]# curl -I http://192.168.235.158 ##查看版本號(hào) HTTP/1.1 200 OK Server: nginx/1.12.0 ##可見版本號(hào)為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選項(xiàng)的值設(shè)置為off即可
jpg
第三步:驗(yàn)證Nginx版本號(hào)隱藏
[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 ##可見版本號(hào)已被隱藏 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
三、修改配置源碼法法隱藏版本號(hào)
第一步:修改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
第四步:驗(yàn)證Nginx版本號(hào)隱藏
[root@localhost nginx-1.12.0]# curl -I http://192.168.235.158 HTTP/1.1 200 OK Server: nginx/1.1.1 ##可見版本號(hào)已成功更改為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)頁緩存時(shí)間實(shí)例演示
第一步:復(fù)制圖片到站點(diǎn)目錄
[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標(biāo)簽下添加圖片路徑
第三步:修改Nginx .conf文件
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf user nginx nginx; ##單獨(dú)輸入此行條目,指定用戶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
第四步:打開一臺(tái)Win10虛擬機(jī)驗(yàn)證
在客戶機(jī)中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網(wǎng)頁
總結(jié)
以上所述是小編給大家介紹的Nginx隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)本站網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。