Nginx隐藏版本号与网页缓存时间的方法

Nginxä¼å---éèçæ¬å·ä¸ç½é¡µç¼å­æ¶é´

éç½®Nginxéèçæ¬å·

å¨ç产ç¯å¢ä¸­ï¼éè¦éèNginxççæ¬å·ï¼ä»¥é¿åå®å¨

æ¼æ´çæ³æ¼

æ¥çæ¹æ³

â使ç¨fiddler Iå·å¨Windows客æ·ç«¯æ¥çNginxçæ¬å·

å¨CentOSç³»ç»ä¸­ä½¿ç¨âcurl -I ç½åâå½ä»¤æ¥ç

Nginxéèçæ¬å·çæ¹æ³

âä¿®æ¹éç½®æ件æ³

âä¿®æ¹æºç æ³

ä¿®æ¹éç½®æ件æ³

1.Nginxçéç½®æ件中çserver_ tokens é项çå¼è®¾ç½®ä¸ºoff

[root@www conf]# vim nginx.conf
.....
server_ tokens off;
.....
[root@www conf]# nginx -t

2.éå¯æå¡ï¼è®¿é®ç½ç«ä½¿ç¨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对åºçå¼ä¿®æ¹ä¸º

fastcgi_ param SERVER_ SOFTWARE nginx ;

ä¿®æ¹æºç æ³

Nginxæºç æ件/usr/src/nginx-1.12.0/src/core/nginx.håå«äºçæ¬ä¿¡æ¯ï¼å¯ä»¥éæ设置éæ°ç¼è¯å®è£ï¼éèçæ¬ä¿¡æ¯

示ä¾:

#define NGINX_ _VERSIONâ1.1.1â ï¼ä¿®æ¹çæ¬å·ä¸º1.1.1
#define NGINX_ VER "IIS/" ï¼ä¿®æ¹è½¯ä»¶ç±»å为IIS

éå¯æå¡ï¼è®¿é®ç½ç«ä½¿ç¨curl -Iå½ä»¤æ£æµ

ä¿®æ¹Nginxç¨æ·ä¸ç»

Nginxè¿è¡æ¶è¿ç¨éè¦æç¨æ·ä¸ç»çæ¯æï¼ä»¥å®ç°å¯¹ç½ç«æ件读åæ¶è¿è¡è®¿é®æ§å¶

Nginxé»è®¤ä½¿ç¨nobodyç¨æ·è´¦å·ä¸ç»è´¦å·ï¼ä¸è¬ä¹è¦è¿è¡ä¿®æ¹

ä¿®æ¹çæ¹æ³

âç¼è¯å®è£æ¶æå®ç¨æ·ä¸ç»
âä¿®æ¹éç½®æ件æå®ç¨æ·ä¸ç»

ä¿®æ¹éç½®æ件æ³æå®

1.æ°å»ºç¨æ·è´¦å·ï¼å¦nginx

2.ä¿®æ¹ä¸»éç½®æ件useré项ï¼æå®ç¨æ·è´¦å·

3.éå¯nginxæå¡ï¼ä½¿éç½®çæ

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ç½é¡µç¼å­æ¶é´

å½Nginxå°ç½é¡µæ°æ®è¿åç»å®¢æ·ç«¯åï¼å¯è®¾ç½®ç¼å­çæ¶é´ï¼ä»¥æ¹ä¾¿å¨æ¥åè¿è¡ç¸åå容ç请æ±æ¶ç´æ¥è¿åï¼é¿åéå¤è¯·æ±ï¼å å¿«äºè®¿é®é度è¬é对éæç½é¡µè®¾ç½®ï¼å¯¹å¨æç½é¡µä¸è®¾ç½®ç¼å­æ¶é´,å¯å¨Windows客æ·ç«¯ä¸­ä½¿ç¨fiddleræ¥çç½é¡µç¼å­æ¶é´

设置æ¹æ³

å¯ä¿®æ¹éç½®æ件ï¼å¨http段ã æèserver段ã æèlocation段å å¥å¯¹ç¹å®å容çè¿æåæ°

示ä¾

ä¿®æ¹Nginxçéç½®æ件ï¼å¨location段å å¥expiresåæ°

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

éèçæ¬å·å®ä¾æ¼ç¤º

ä¸ãç¼è¯å®è£Nginxæå¡

第ä¸æ­¥:è¿ç¨è·å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    //å缩å½æ°åº

第åæ­¥:å建ç¨åºç¨æ·å¹¶éç½®Nginxæå¡ç¸å³ç»ä»¶

[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//å建ç¨åºç¨æ·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
//å®è£ç¶æç»è®¡æ¨¡å

第äºæ­¥:ç¼è¯ä¸å®è£Nginx

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

第å­æ­¥:ä¼åNginxæå¡å¯å¨èæ¬,并建ç«å½ä»¤è½¯è¿æ¥

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
//å建nginxæå¡å½ä»¤è½¯é¾æ¥å°ç³»ç»å½ä»¤
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service 
//å³é­é²ç«å¢
[root@localhost nginx-1.12.0]# setenforce 0
//å³é­å¢å¼ºåå®å¨åè½
[root@localhost nginx-1.12.0]# nginx 
//è¾å¥nginx å¼å¯æå¡
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80   //æ¥çæå¡ç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   ##å建éç½®æ件

[Unit]
Description=nginx                      ##æè¿°
After=network.target                    ##æè¿°æå¡ç±»å
[Service]
Type=forking                          ##åå°è¿è¡å½¢å¼
PIDFile=/usr/local/nginx/logs/nginx.pid      ##PIDæ件ä½ç½®
ExecStart=/usr/local/nginx/sbin/nginx       ##å¯å¨æå¡
ExecReload=/usr/bin/kill -s HUP $MAINPID  ##æ ¹æ®PIDéè½½éç½®
ExecStop=/usr/bin/kill -s QUIT $MAINPID    ##æ ¹æ®PIDç»æ­¢è¿ç¨
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service   ##设置æ§è¡æé
[root@localhost ~]# systemctl stop nginx.service    ##å³é­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å议段è½ä¸­å å¥server_ tokensé项çå¼è®¾ç½®ä¸º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

ç½é¡µç¼å­æ¶é´å®ä¾æ¼ç¤º

第ä¸æ­¥:å¤å¶å¾çå°ç«ç¹ç®å½

[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ç½é¡µ

[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ç½é¡µ

Nginx隐藏版本号与网页缓存时间的方法

æ»ç»

以ä¸æè¿°æ¯å°ç¼ç»å¤§å®¶ä»ç»çNginxéèçæ¬å·ä¸ç½é¡µç¼å­æ¶é´ï¼å¸æ对大家ææ帮å©ï¼å¦æ大家æä»»ä½çé®è¯·ç»æçè¨ï¼å°ç¼ä¼åæ¶åå¤å¤§å®¶çãå¨æ­¤ä¹é常æ谢大家对èæ¬ä¹å®¶ç½ç«çæ¯æï¼
å¦æä½ è§å¾æ¬æ对你æ帮å©ï¼æ¬¢è¿è½¬è½½ï¼ç¦è¯·æ³¨æåºå¤ï¼è°¢è°¢ï¼

相关推荐