一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

云服務(wù)器|WEB服務(wù)器|FTP服務(wù)器|郵件服務(wù)器|虛擬主機(jī)|服務(wù)器安全|DNS服務(wù)器|服務(wù)器知識(shí)|Nginx|IIS|Tomcat|

服務(wù)器之家 - 服務(wù)器技術(shù) - Nginx - Nginx隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間的方法

Nginx隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間的方法

2020-01-20 11:56caozhengtao1213 Nginx

這篇文章主要介紹了Nginx優(yōu)化之隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

?
1
2
3
4
5
[root@www conf]# vim nginx.conf
.....
server_ tokens off;
.....
[root@www conf]# nginx -t

2.重啟服務(wù),訪問網(wǎng)站使用curl -I命令檢測

?
1
2
3
4
[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對應(yīng)的值修改為

?
1
fastcgi_ param SERVER_ SOFTWARE nginx ;

修改源碼法

Nginx源碼文件/usr/src/nginx-1.12.0/src/core/nginx.h包含了版本信息,可以隨意設(shè)置重新編譯安裝,隱藏版本信息

示例:

?
1
2
#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)對網(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)改變效果

?
1
2
3
4
5
6
7
[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)容的請求時(shí)直接返回,避免重復(fù)請求,加快了訪問速度般針對靜態(tài)網(wǎng)頁設(shè)置,對動(dòng)態(tài)網(wǎng)頁不設(shè)置緩存時(shí)間,可在Windows客戶端中使用fiddler查看網(wǎng)頁緩存時(shí)間

設(shè)置方法

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

示例

修改Nginx的配置文件,在location段加入expires參數(shù)

?
1
2
3
location ~ \.(gifjpgliepglpnglbmplico)$ {
root html;
expires 1d;

隱藏版本號(hào)實(shí)例演示

一、編譯安裝Nginx服務(wù)

第一步:遠(yuǎn)程獲取Windows上的源碼包,并掛載到Linux上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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

第二步:解壓源碼包

?
1
2
3
4
[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

第三步:下載安裝編譯組件包

?
1
2
3
4
5
6
[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)組件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[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

?
1
[root@localhost nginx-1.12.0]# make && make install

第六步:優(yōu)化Nginx服務(wù)啟動(dòng)腳本,并建立命令軟連接

?
1
2
3
4
5
6
7
8
9
10
[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腳本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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)

?
1
2
3
4
5
6
7
8
9
10
11
[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配置文件

?
1
2
3
4
5
6
7
[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)隱藏

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[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配置文件

?
1
2
3
4
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
 server_tokens on;
 ##將off替換成on

第二步:修改源碼文件nginx.h中的版本信息

?
1
2
3
4
[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h
 
#define NGINX_VERSION   "1.1.1"
##更改版本信息為1.1.1

第三步:重新編譯Nginx

?
1
2
3
4
5
6
7
[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)隱藏

?
1
2
3
4
5
6
7
8
9
10
11
[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)目錄

?
1
2
3
4
5
6
7
8
9
[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)頁

 

?
1
2
3
4
5
[root@localhost html]# vim index.html
 
<h1>Welcome to nginx!</h1>
<img src="game.jpg"/>
##在h1標(biāo)簽下添加圖片路徑

 

第三步:修改Nginx .conf文件

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[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)頁

Nginx隱藏版本號(hào)與網(wǎng)頁緩存時(shí)間的方法

總結(jié)

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

原文鏈接:https://blog.51cto.com/14449521/2450139

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品在线观看一区 | 4455永久在线观免费看片 | 日韩免费一级 | 日韩欧美亚洲一区二区综合 | 色婷婷影院在线视频免费播放 | 日韩欧美精品一区二区 | 狠狠色综合久久久久尤物 | 91久久线看在观草草青青 | 亚洲另类老妇videos | 欧美一级高清免费a | 日本无翼乌漫画 | 经典千人斩一区二区视频 | 欧美视频久久 | 亚洲AV精品无码喷水直播间 | 惩罚狠h调教灌满 | 精品国内自产拍在线视频 | chinese真实incest chinese特色video chinese男性厕所撒尿合集 | 女人日男人 | 青草视频网址 | 免费观看www视频 | 精品国产人妻国语 | 魔法满屋免费观看完整版中文 | 日日操免费视频 | 极品美女aⅴ高清在线观看 极品ts赵恩静和直男激战啪啪 | 成人男女网免费 | 日本天堂视频在线观看 | 精品午夜寂寞影院在线观看 | 成人18视频在线观看 | 猫影视tv接口 | 日本一道本中文字幕 | 亚洲精品久久玖玖玖玖 | 国产麻豆在线观看网站 | 色就色综合 | 草草免费观看视频在线 | 午夜精品久久久 | 欧美a级在线观看 | 日本最新伦中文字幕 | 国产在线视频欧美亚综合 | 欧美3d怪物交videos网站 | 女同性互吃奶乳免费视频 | 四虎在线永久免费视频网站 |