Nginx首部相關模塊使用介紹-隱藏,修改

ngx_http_headers_module模塊
一. 前言
ngx_http_headers_module模塊提供了兩個重要的指令add_header和expires,來添加 “Expires” 和 “Cache-Control” 頭字段,對響應頭添加任何域字段。add_header可以用來標示請求訪問到哪臺服務器上,這個也可以通過nginx模塊nginx-http-footer-filter研究使用來實現。expires指令用來對瀏覽器本地緩存的控制。
二. add_header指令
語法: add_header name value;
默認值: —
配置段: http, server, location, if in location
對響應代碼為200,201,204,206,301,302,303,304,或307的響應報文頭字段添加任意域。如:

add_header From jb51.net
三. expires指令
語法: expires [modified] time;
expires epoch | max | off;
默認值: expires off;
配置段: http, server, location, if in location
在對響應代碼為200,201,204,206,301,302,303,304,或307頭部中是否開啟對“Expires”和“Cache-Control”的增加和修改操作。
可以指定一個正或負的時間值,Expires頭中的時間根據目前時間和指令中指定的時間的和來獲得。
epoch表示自1970年一月一日00:00:01 GMT的絕對時間,max指定Expires的值為2037年12月31日23:59:59,Cache-Control的值為10 years。
Cache-Control頭的內容隨預設的時間標識指定:
·設置為負數的時間值:Cache-Control: no-cache。
·設置為正數或0的時間值:Cache-Control: max-age = #,這里#的單位為秒,在指令中指定。
參數off禁止修改應答頭中的”Expires”和”Cache-Control”。
實例一:對圖片,flash文件在瀏覽器本地緩存30天

1
2
3
4
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 {
      expires 30d;
 }

實例二:對js,css文件在瀏覽器本地緩存1小時

1
2
3
4
location ~ .*\.(js|css)$
 {
      expires 1h;
 }


proxy_hide_header 模塊

一.介紹proxy_hide_header 模塊

proxy_hide_header 模塊是當Nginx用來當代理服務器是隱藏后端服務器首部文件的模塊,默認隱藏“Date”, “Server”, “X-Pad”, and “X-Accel-…” 等模塊。

默認情況下Nginx不會傳遞后端服務器的Date,Server等首部信息

ngx_headers_more模塊
一. 介紹ngx_headers_more
ngx_headers_more 用于添加、設置和清除輸入和輸出的頭信息。nginx源碼沒有包含該模塊,需要另行添加。
該模塊是ngx_http_headers_module模塊的增強版,提供了更多的實用工具,比如復位或清除內置頭信息,如Content-Type, Content-Length, 和Server。
可以允許你使用-s選項指定HTTP狀態碼,使用-t選項指定內容類型,通過more_set_headers 和 more_clear_headers 指令來修改輸出頭信息。如:

1
more_set_headers -s 404 -t ‘text/html’ ‘X-Foo: Bar’;

輸入頭信息也可以這么修改,如:

1
2
3
4
5
location /foo {
  more_set_input_headers ‘Host: foo’ ‘User-Agent: faked’;
  # now $host, $http_host, $user_agent, and
  #  $http_user_agent all have their new values.
}

-t選項也可以在more_set_input_headers和more_clear_input_headers指令中使用。
不像標準頭模塊,該模塊的指示適用于所有的狀態碼,包括4xx和5xx的。 add_header只適用于200,201,204,206,301,302,303,304,或307。

二. 安裝ngx_headers_more

1
2
3
4

5

6

 wget ‘http://nginx.org/download/nginx-1.11.2.tar.gz’ 
tar -xzvf nginx-1.11.2.tar.gz 
cd nginx-1.11.2/
 # Here we assume you would install you nginx under /opt/nginx/.
 ./configure –prefix=/opt/nginx  –add-module=/path/to/headers-more-nginx-module
make && make install

ngx_headers_more 包下載地址:https://github.com/openresty/headers-more-nginx-module
ngx_openresty包含該模塊。
三. 指令說明
more_set_headers
語法:more_set_headers [-t <content-type list>]… [-s <status-code list>]… <new-header>…
默認值:no
配置段:http, server, location, location if
階段:輸出報頭過濾器
替換(如有)或增加(如果不是所有)指定的輸出頭時響應狀態代碼與-s選項相匹配和響應的內容類型的-t選項指定的類型相匹配的。
如果沒有指定-s或-t,或有一個空表值,無需匹配。因此,對于下面的指定,任何狀態碼和任何內容類型都講設置。

1
more_set_headers  “Server: my_server”;

具有相同名稱的響應頭總是覆蓋。如果要添加頭,可以使用標準的add_header指令代替。
單個指令可以設置/添加多個輸出頭。如:

1
more_set_headers ‘Foo: bar’ ‘Baz: bah’;

在單一指令中,選項可以多次出現,如:

1
more_set_headers -s 404 -s ‘500 503’ ‘Foo: bar’;

等同于:

1
more_set_headers -s ‘404 500 503’ ‘Foo: bar’;

新的頭是下面形式之一:

  • Name: Value
  • Name:
  • Name

最后兩個有效清除的頭名稱的值。Nginx的變量允許是頭值,如:

1
2
set $my_var “dog”;
more_set_headers “Server: $my_var”;

注意:more_set_headers允許在location的if塊中,但不允許在server的if塊中。下面的配置就報語法錯誤:

1
2
3
4
5
6
7
8
# This is NOT allowed!
 server {
    if ($args ~ ‘download’) {
      more_set_headers ‘Foo: Bar’;
    }
    …
  }
more_clear_headers

語法:more_clear_headers [-t <content-type list>]… [-s <status-code list>]… <new-header>…
默認值:no
配置段:http, server, location, location if
階段:輸出報頭過濾器
清除指定的輸出頭。

1
more_clear_headers -s 404 -t ‘text/plain’ Foo Baz;

等同于

1
more_set_headers -s 404 -t ‘text/plain’ “Foo: ” “Baz: “;

1
more_clear_headers -s 404 -t ‘text/plain’ Foo Baz;

等同于

1
more_set_headers -s 404 -t ‘text/plain’ “Foo: ” “Baz: “;

1
more_set_headers -s 404 -t ‘text/plain’ Foo Baz

也可以使用通配符*,如:

1
more_clear_headers ‘X-Hidden-*’;

清除開始由“X-Hidden-”任何輸出頭。
more_set_input_headers
語法:more_set_input_headers [-r] [-t <content-type list>]… <new-header>…
默認值:no
配置段:http, server, location, location if
階段: rewrite tail
非常類似more_set_headers,不同的是它工作在輸入頭(或請求頭),它僅支持-t選項。
注意:使用-t選項的是過濾請求頭的Content-Type,而不是響應頭的。
more_clear_input_headers
語法:more_clear_input_headers [-t <content-type list>]… <new-header>…
默認值:no
配置段:http, server, location, location if
階段: rewrite tail
清除指定輸入頭。如:

1
more_clear_input_headers -s 404 -t ‘text/plain’ Foo Baz;

等同于

1
more_set_input_headers -s 404 -t ‘text/plain’ “Foo: ” “Baz: “;

1
more_clear_input_headers -s 404 -t ‘text/plain’ Foo Baz;

等同于

1
more_set_input_headers -s 404 -t ‘text/plain’ “Foo: ” “Baz: “;

1
more_set_input_headers -s 404 -t ‘text/plain’ Foo Baz

四. ngx_headers_more局限性
1. 不同于標準頭模塊,該模塊不會對下面頭有效: Expires, Cache-Control, 和Last-Modified。
2. 使用此模塊無法刪除Connection的響應報頭。唯一方法是更改src/ HTTP/ ngx_http_header_filter_module.c文件。
五. 使用ngx_headers_more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# set the Server output header
more_set_headers ‘Server: my-server’;
  
# set and clear output headers
location /bar {
  more_set_headers ‘X-MyHeader: blah’ ‘X-MyHeader2: foo’;
  more_set_headers -t ‘text/plain text/css’ ‘Content-Type: text/foo’;
  more_set_headers -s ‘400 404 500 503’ -s 413 ‘Foo: Bar’;
  more_clear_headers ‘Content-Type’;
  
  # your proxy_pass/memcached_pass/or any other config goes here…
}
  
# set output headers
location /type {
  more_set_headers ‘Content-Type: text/plain’;
  # …
}
  
# set input headers
location /foo {
  set $my_host ‘my dog’;
  more_set_input_headers ‘Host: $my_host’;
  more_set_input_headers -t ‘text/plain’ ‘X-Foo: bah’;
  
  # now $host and $http_host have their new values…
  # …
}
  
# replace input header X-Foo *only* if it already exists
more_set_input_headers -r ‘X-Foo: howdy’;

六. 應用ngx_headers_more
修改web服務器是什么軟件,什么版本,同時隱藏Centent-Type、Accept-Range、Content-Length頭信息。

2016113161809381.jpg (861×382)

1
2
3
4
more_set_headers “Server: jb51.net Web Server”;
more_clear_headers “Content-Type:”;
more_clear_headers “Accept-Ranges: “;
more_clear_headers “Content-Length: “;

2016113161847323.jpg (827×380)

404狀態碼添加header
配置如下:

1
2
3
4
5
more_set_headers “Server: jb51.net Web Server”;
more_set_headers -s 404 “Error: Not found”;
more_clear_headers “Content-Type:”;
more_clear_headers “Accept-Ranges: “;
more_clear_headers “Content-Length: “;

2016113161914791.jpg (836×354)

原創文章,作者:cnc,如若轉載,請注明出處:http://www.www58058.com/74839

(0)
cnccnc
上一篇 2017-05-06
下一篇 2017-05-06

相關推薦

  • 馬哥教育網絡版22期+第10周作業

    week10 1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) 2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;   (1) 為硬盤新建兩個主分區;并為其安裝grub;   (2) 為硬盤的第一個主分區提供內核和ramdisk文件;&nbsp…

    Linux干貨 2016-10-29
  • 配置使用基于mysql存儲rsyslog日志信息

    配置使用基于mysql存儲rsyslog日志信息   日志對于我們來說,肯定不會陌生。每個系統應用,只要有人訪問,每時每刻都會產生大量的日志,用來記錄服務器應用的運行信息。以便于我們在服務有異常時進行查看,或是從日志記錄中提取出應用系統的運行信息。某些電商Web網站甚至會利用日志記錄去對用戶的購買行為作分析,以便更好的服務于客戶。 &nb…

    Linux干貨 2016-09-05
  • N26——第三周作業

    一、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可 [root@localhost ~]# who | cut -d ' ' -f1 | uniq root zhaoyujia 二、取出最后登錄到當前系…

    Linux干貨 2017-01-14
  • locate與find不得不說的事

    本文內容: locate與find 練習     locate與find是linux中最常用的兩種查找方式,二者各有優缺點,locate查找迅速,卻由于數據庫非實時更新,導致可能查找不全,而find由于是從指定路徑開始遍歷,速度相對較慢,卻更為準確,而且用法也更加靈活,因此find運用更加廣泛,也是本文的重點。 文件查找: locate搜…

    Linux干貨 2016-08-18
  • yum命令用法及源碼的編譯

     一:yum 何為yum? yum是rpm的前端程序,可解決軟件包相關依賴性,可在多個庫之間定位軟件包,是up2date 的替代工具。 1.命令格式:     yum [options] [command] [package …] 2.yum客戶端配置文件: 公共配置文件:/etc/yum.conf   &…

    Linux干貨 2017-08-19
  • Nginx 進階 (ssl、fpm、rewrite、cache配置等)

    Nginx(與ssl結合配置https網站、rewrite,fastcgi配置詳解) 前言 前面已經介紹過Nginx的一些基礎概念,還有幾個比較重要的模塊:利用ssl給會話加密,利用rewrite功能靈活改寫訪問結果,以及利用fastcgi與php模塊結合等等。 一、配置https網站 1、自建CA (1)生成私鑰文件 mkdir -p /etc/pki/C…

    Linux干貨 2016-12-26
欧美性久久久久