nginx修改响应头(可屏蔽后端服务器的信息:IIS,PHP等)

本文介绍了如何通过nginx的headers-more-nginx-module模块修改反向代理请求的Header,以此来屏蔽后端如IIS、PHP服务器的信息。详细讲解了`more_set_headers`指令的使用,并提供了模块的安装方法和相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改nginx反向代理请求的Header

需要使用到proxy_set_headeradd_header指令。其中:

proxy_set_header 来自内置模块ngx_http_proxy_module
用来重定义发往代理服务器服务器的请求头。参考:https://blog.youkuaiyun.com/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';

 

转载于:https://www.cnblogs.com/dingzp/p/11004773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值