www.cnblogs.com/ruiyqinrui

开源、架构、Linux C/C++/python AI BI 运维开发自动化运维。 春风桃李花 秋雨梧桐叶。“力尽不知热 但惜夏日长”。夏不惜,秋不获。@ruiY--秦瑞

python爬虫,C编程,嵌入式开发.hadoop大数据,桉树,onenebula云计算架构.linux运维及驱动开发.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  2912 随笔 :: 9 文章 :: 51 评论 :: 185万 阅读
复制代码
修改nginx反向代理请求的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
$  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';
复制代码

 

posted on   秦瑞It行程实录  阅读(376)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2021-12-01 vc qt dll
2021-12-01 qoci 编译完 放置位置 具体根据情况
2021-12-01 qt 开发发布于 windeploy.exe
2021-12-01 qt oracle
2021-12-01 QOCIDriver unable to create environment
2021-12-01 qt qoci 测试验证
2021-12-01 calling 'lastError' with incomplete return type 'QSqlError' qsqlquer
www.cnblogs.com/ruiyqinrui
点击右上角即可分享
微信分享提示