Nginx裏Header修改

有時候,咱們可能有修改Nginx默認Header的需求。本文就將常見的方法列出來供你們參考。php

修改普通請求的Header

Nginx內置的模塊暫時僅支持修改響應頭,使用add_header。其中:css

add_header 來自內置模塊ngx_http_headers_module,用於設置response header。參考:http://www.cnblogs.com/linxiong945/p/4174262.htmlhtml

若是須要設置普通請求的request header,則須要單獨安裝headers-more-nginx-module模塊。該模塊提供了more_set_headersmore_set_input_headers分別用於設置請求、響應頭。nginx

示例:git

location ~ .*\.(php|php5)?$ {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param LOG_ID $request_id;
    more_set_input_headers "Cookie: name=hello";
    more_set_headers "X-Powered-By:PHP";
    add_header  X-Powered-By2 'PHP';
    
    include fastcgi.conf;
}

修改反向代理請求的Header

須要使用到proxy_set_headeradd_header指令。其中:github

proxy_set_header 來自內置模塊ngx_http_proxy_module
用來重定義發往代理服務器服務器的請求頭。參考:https://blog.csdn.net/weixin_41585557/article/details/82426784bash

示例:服務器

location  ^~/test/ {
    proxy_pass http://127.0.0.1:8001$request_uri;
    proxy_set_header host $http_host;
    proxy_set_header x-real-ip $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
}

headers-more-nginx-module 模塊

headers-more-nginx-module 模塊用於添加、修改或清除 請求/響應頭,該模塊不是nginx自帶的,默認不包含該模塊,須要另外安裝。memcached

Github地址:https://github.com/openresty/headers-more-nginx-module.net

安裝:

$  wget 'http://nginx.org/download/nginx-1.13.6.tar.gz'
$  tar -xzvf nginx-1.13.6.tar.gz
$  cd nginx-1.13.6/

# 假設Nginx安裝在 /opt/nginx/ 目錄
$  ./configure --prefix=/opt/nginx \
     --add-module=/path/to/headers-more-nginx-module

$  make -j2
$  make install

從 NGINX 1.9.11 開始,能夠使用動態模塊加載(生成.so文件,無需重啓Nginx整個服務):

$ ./configure --prefix=/opt/nginx \
     --add-dynamic-module=/path/to/headers-more-nginx-module

在Nginx配置文件里加上:

load_module /path/to/modules/ngx_http_headers_more_filter_module.so;

具體安裝流程及細節步驟參考:Nginx安裝echo模塊 http://www.javashuo.com/article/p-geevpvfz-gr.html 。由於是相似的。

該模塊主要有4個指令:

  • more_set_headers 用於添加、修改、清除響應頭
  • more_clear_headers 用於清除響應頭
  • more_set_input_headers 用於添加、修改、清除請求頭
  • more_clear_input_headers 用於清除請求頭

示例:

# 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';

參考

一、【隨筆】nginx add_header指令的使用 - linxiong - 博客園
http://www.cnblogs.com/linxiong945/p/4174262.html
二、nginx的headers_more模塊的使用 - chunyuan314的博客 - CSDN博客
https://blog.csdn.net/chunyuan314/article/details/81737303
三、關於nginx中proxy_set_header的設置 - 七號空間 - CSDN博客
https://blog.csdn.net/weixin_41585557/article/details/82426784

相關文章
相關標籤/搜索