nginx---隱藏或添加后端服務器的信息


proxy_hide_header field;
  用於隱藏后端服務器特定的響應首部,默認nginx在響應報文中不傳遞后端服務器的首部字段Date, Server, X-Pad, X-Accel等

測試訪問:

curl www.test.net/api/m.html -L -I
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Tue, 08 Jun 2021 06:25:15 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 601019
Connection: keep-alive
Last-Modified: Tue, 08 Jun 2021 05:06:29 GMT
ETag: "92bbb-5c43a1ef0c1ab"
Accept-Ranges: bytes
ETag: "92bbb-5c43a1ef0c1ab" 這一段不希望被別人看到
  1、修改配置文件
vim /etc/nginx/conf.d/test.conf 

在server字段,任意位置添加如下:

  proxy_hide_header Etag;

  2、重啟服務

1 nginx -s stop 
2 nginx

  3、測試訪問

curl www.test.net/api/m.html -L -I
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Tue, 08 Jun 2021 06:29:43 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 601019
Connection: keep-alive
Last-Modified: Tue, 08 Jun 2021 05:06:29 GMT
Accept-Ranges: bytes

proxy_pass_header field;
  默認nginx在響應報文中不傳遞后端服務器的首部字段Date, Server, X-Pad, X-Accel等參數,如果要傳遞的話則要使用 proxy_pass_header field聲明將后端服務器返回的值傳遞給客戶端

  

 1、修改配置文件
vim /etc/nginx/conf.d/test.conf 

在server字段,任意位置添加如下:

  proxy_hide_header Server;

  2、重啟服務

1 nginx -s stop 
2 nginx

  3、測試訪問

curl www.test.net/api/m.html -L -I
HTTP/1.1 200 OK
Date: Tue, 08 Jun 2021 06:32:42 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 601019
Connection: keep-alive
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 08 Jun 2021 05:06:29 GMT
Accept-Ranges: bytes

會顯示后端服務器的版本號


 

自定義緩存信息:
  查看緩存的命中率:add_header X-Cache $upstream_cache_status; 緩存的命中率
在server字段,任意位置添加如下:
add_header X-Cache $upstream_cache_status; 

1、測試訪問

curl www.test.net/api/m.html -L -I
HTTP/1.1 200 OK
Date: Tue, 08 Jun 2021 07:06:58 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 601019
Connection: keep-alive
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 08 Jun 2021 05:06:29 GMT
X-Cache: MISS
Accept-Ranges: bytes

2、再次測試訪問

curl www.test.net/api/m.html -L -I
HTTP/1.1 200 OK
Date: Tue, 08 Jun 2021 07:08:09 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 601019
Connection: keep-alive
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 08 Jun 2021 05:06:29 GMT
X-Cache: HIT
Accept-Ranges: bytes

  第一次沒有利用緩存,后面緩存利用率就是HIT了

  保持訪問響應緩存的狀態(0.8.3)。狀態可以是 “MISS”, “BYPASS”, “EXPIRED”, “STALE”, “UPDATING”, “REVALIDATED”, or “HIT”.

代理服務器nginx的IP地址:add_header X-Via $server_addr;
代理服務器的主機名:add_header X-Accel $server_name;
 X-Via和X-Accel都是自定義的字段,可以自己隨便定義

  add_trailer name value [always];
  添加自定義響應信息的尾部, 1.13.2版后支持

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM