修改nginx反向代理请求的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
$ make install

在Nginx配置文件里加上:

load_module /path/to/modules/ngx_http_headers_more_filter_module.so;
该模块主要有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. 极路由访问Apple Store可以浏览但是不能下载的解决方案
  2. MVC 微信扫码支付
  3. 获取网络状态ios(2G、3G、4G、Wifi)
  4. 原生视觉差滚动---js+css;
  5. Delphi Waring 的信息
  6. js导入插件注意事项.txt
  7. Python之路:爬虫之urllib库的基本使用和高级使用
  8. [实战演练]python3使用requests模块爬取页面内容
  9. html2canvas不能识别svg的解决方案
  10. python之路--day8---day9--两日内容
  11. HTTP协议详解(二)
  12. 视频剪辑软件原型-videocut
  13. RHEL7 下双网卡绑定做主备(冗余)
  14. Redis初探-Redis安装
  15. Qt程序ibus输入法不跟随
  16. JDK8 - Function介绍
  17. Android oncreate onupgrade什么时候被调用
  18. Ajax-java中的ajax使用,以及编码问题
  19. 使用GDI技术创建ASP.NET验证码
  20. 《Pro Git》第3章 分支

热门文章

  1. python 图形
  2. MAC 上抓取网页数据的工具有哪些?
  3. Spark(四十六):Spark 内存管理之—OFF_HEAP
  4. 解决request.getSession().getServletContext().getRealPath("/")为null问题
  5. 脸型分类-Face shape classification using Inception v3
  6. 漏洞分析:OpenSSH用户枚举漏洞(CVE-2018-15473)分析
  7. JS数组常见方法的深浅拷贝分类
  8. SpringMvc使用JavaMail发送邮件,并使用465端口开启ssl加密传输,保证邮件安全
  9. markdown如何在表格内换行?
  10. AndoridSQLite数据库开发基础教程(4)