Nginx里Header修改
有时候,我们可能有修改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_headers,more_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_header和add_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修改的更多相关文章
- Nginx通过header转发
假设添加自定义头 "my-header",当"my-header"等于test时,转发到192.168.1.113 请求如下 wget --header=&qu ...
- EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态
本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...
- chrome http Request Header 修改插件
chrome http Request Header 修改插件 2013-05-31 11:03:03| 分类: JavaScript | 标签:chrome extensions chang ...
- SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值?
SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值? 记得在D项目上线之前数据导入系统之后,业务提出一些物料采购视图里的’Purchasing value k ...
- Nginx里的root/index/alias/proxy_pass的意思
1.[alias] 别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向[alias]配置的路径.如: location /test/ { alias /home/sftp/i ...
- .net 4.0 以下HttpWebRequest Header 修改hosts方法
.net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录 public class CusteredHeaderCollection : WebHeaderCol ...
- docker+nginx 安装部署修改资源目录配置文件和容器端口信息
查看docker镜像 可以先查看docker下是否存在nginx镜像,使用如下这些命令查看: docker images: 列出所有镜像. docker images nginx: 列出所有nginx ...
- 修改Nginx的header伪装服务器
有时候为了伪装自己的真实服务器环境.不像让对方知道自己的webserver真实环境,就不得不修改我们的webserer软件了!今天看了一下baidu.com的webserver感觉像是nginx修改的 ...
- nginx 代理之修改header 的HOST,实现代理转代理
现有一个需求,需要从nginx跳转到k8s的traefik代理上,从而实现服务的访问,用于测试.直接修改proxy_set_header的HOST,修改为traefuk代理的域名,proxy_pass ...
随机推荐
- 浅谈nodejs和php
现在,Web开发公司和开发人员可以选择多种技术栈来构建Web应用程序.早期网络发展,不同的技术被用于前端和后端开发.但是,随着Node.js的发布,布局发生了变化,因为它允许开发人员使用 JavaSc ...
- 利用AnyProxy代理监控APP流量
1.介绍 AnyProxy 是阿里巴巴基于 Node.js 开发的一款开源代理服务器. 代理服务器站在客户端和服务端的中间,它可以收集双方通信的每个比特.一个完整的代理请求过程为:客户端首先与代理服务 ...
- JAVA 8 日期工具类
JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...
- DOTween的基本用法
首先声明一点,不要简单的认为 DOTween 只能用在 Transform 组件上完成一些简单的动画,或者是完成一些 UI 动画,DOTween 的用途是很广的,unity中有很多组件都可以使用 DO ...
- [转]构建高性能MySQL体系
来源:http://www.yunweipai.com/archives/21232.html 构建高性能MySQL系统涵盖从单机.硬件.OS.文件系统.内存到MySQL 本身的配置,以及schema ...
- [swarthmore cs75] Lab 0 Warmup & Basic OCaml
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第1次大作业. 什么是编译 编译就是执行Program->Program'转换的过程,如下 ...
- Convert Spaces to Tabs
:set tabstop=2 " To match the sample file :set noexpandtab " Use tabs, not spaces :%retab! ...
- ubuntu installs matlab2017a
cd mkdir matlab sudo mount -o loop *1.iso matlab sudo ./matlab/install ... sudo mount -o loop *2.iso ...
- poj 3159
差分约束 我也忘了谁说的了,反正我记得有人说过,只要是差分约束问题都可以转换成图 至少目前看来都是这样的 我一开始spfa+queue超时 看别人博客才知道要用spfa+stack,感觉智商又下降了一 ...
- 6-使用requests库封装类处理get/post请求
1.request安装 1)pip安装,直接pip install requests 2)下载离线包安装,加压后,命令行进入路径,执行python setup.py install 2.创建工程 注意 ...