有时候,我们可能有修改Nginx默认Header的需求。本文就将常见的方法列出来供大家参考。

修改普通请求的Header

Nginx内置的模块暂时仅支持修改响应头,使用add_header。其中:

add_header 来自内置模块ngx_http_headers_module,用于设置response header。参考:http://www.cnblogs.com/linxiong945/p/4174262.html

如果需要设置普通请求的request header,则需要单独安装headers-more-nginx-module模块。该模块提供了more_set_headersmore_set_input_headers分别用于设置请求、响应头。

示例:

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指令。其中:

proxy_set_header 来自内置模块ngx_http_proxy_module

用来重定义发往代理服务器服务器的请求头。参考:https://blog.csdn.net/weixin_41585557/article/details/82426784

示例:

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自带的,默认不包含该模块,需要另外安装。

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

安装:

$  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模块 https://www.cnblogs.com/52fhy/p/10166333.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';

参考

1、【随笔】nginx add_header指令的使用 - linxiong - 博客园

http://www.cnblogs.com/linxiong945/p/4174262.html

2、nginx的headers_more模块的使用 - chunyuan314的博客 - CSDN博客

https://blog.csdn.net/chunyuan314/article/details/81737303

3、关于nginx中proxy_set_header的设置 - 七号空间 - CSDN博客

https://blog.csdn.net/weixin_41585557/article/details/82426784

Nginx里Header修改的更多相关文章

  1. Nginx通过header转发

    假设添加自定义头 "my-header",当"my-header"等于test时,转发到192.168.1.113 请求如下 wget --header=&qu ...

  2. EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态

    本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...

  3. chrome http Request Header 修改插件

    chrome http Request Header 修改插件 2013-05-31 11:03:03|  分类: JavaScript |  标签:chrome  extensions  chang ...

  4. SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值?

    SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值? 记得在D项目上线之前数据导入系统之后,业务提出一些物料采购视图里的’Purchasing value k ...

  5. Nginx里的root/index/alias/proxy_pass的意思

    1.[alias] 别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向[alias]配置的路径.如: location /test/ { alias /home/sftp/i ...

  6. .net 4.0 以下HttpWebRequest Header 修改hosts方法

    .net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录 public class CusteredHeaderCollection : WebHeaderCol ...

  7. docker+nginx 安装部署修改资源目录配置文件和容器端口信息

    查看docker镜像 可以先查看docker下是否存在nginx镜像,使用如下这些命令查看: docker images: 列出所有镜像. docker images nginx: 列出所有nginx ...

  8. 修改Nginx的header伪装服务器

    有时候为了伪装自己的真实服务器环境.不像让对方知道自己的webserver真实环境,就不得不修改我们的webserer软件了!今天看了一下baidu.com的webserver感觉像是nginx修改的 ...

  9. nginx 代理之修改header 的HOST,实现代理转代理

    现有一个需求,需要从nginx跳转到k8s的traefik代理上,从而实现服务的访问,用于测试.直接修改proxy_set_header的HOST,修改为traefuk代理的域名,proxy_pass ...

随机推荐

  1. ajax 参数记录

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  2. 区域检测算法-MSERs

    区域检测算法-MSERs:最大稳定极值区域 参考书籍——<图像局部不变性特征与描述>王永明.王贵锦著 MSER最大极值稳定区域的提取步骤:1.像素点排序   2.极值区域生成   3.稳定 ...

  3. 2019.03.25 bzoj4539: [Hnoi2016]树(主席树+倍增)

    传送门 题意:给一棵大树,令一棵模板树与这棵树相同,然后进行mmm次操作,每次选择模板树中的一个节点aaa和大树中一个节点bbb,把aaa这棵子树接在bbb上面,节点编号顺序跟aaa中的编号顺序相同. ...

  4. angular 实现 echarts 拖动区域进行放大 方法

    实现逻辑: 1.通过鼠标摁下事件  和弹出事件  获取x轴的index  之后去x轴的list中去获取两个坐标点 2.之后将这两个数据作为参数  传到后台更新数据 3.记录下来这两个坐标点 放到lis ...

  5. Elasticsearch System Call Filters Failed to Install

    Elasticsearch starts to run, error occurs: : system call filters failed to install; check the logs a ...

  6. css中文字超出文本框,溢出部分用点点点表示

        text-overflow 属性规定当文本溢出包含元素时发生的事情.我们可以使用它来对文本超出的部分进行样式的处理. text-overflow: clip|ellipsis|string;包 ...

  7. Breathe me

    Help, I have done it again 帮帮我,我又做错了. I have been here many times before 哪怕这已经不是一两次了. Hurt myself ag ...

  8. 8-unittest中case管理

    1.关联 在接口测试中难免碰到接口B的参数值来源于接口A的返回结果,此现象即为关联.在unittest中怎么处理这种情况呢?此问题通过全局变量来解决,将变量定义为全局变量:globals()[‘var ...

  9. Ubuntu 16——安装——ns2.35和nam

    Ubuntu 16.04 安装ns2.35+nam 总结出以下安装步骤 1: 更新源 sudo apt-get update #更新源列表 sudo apt-get upgrade #更新已经安装的包 ...

  10. 可参数化的带优先级的数据选择器的FPGA实现方式探讨

    在FPGA设计中,大部分情况下我们都得使用到数据选择器.并且为了设计参数化,可调,通常情况下我们需要一个参数可调的数据选择器,比如M选1,M是可调的参数. 如果,数据选择器是不带优先级的,我们可以使用 ...