nginx修改响应头(可屏蔽后端服务器的信息:IIS,PHP等)
2024-08-26 16:10:21
修改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';
最新文章
- 极路由访问Apple Store可以浏览但是不能下载的解决方案
- MVC 微信扫码支付
- 获取网络状态ios(2G、3G、4G、Wifi)
- 原生视觉差滚动---js+css;
- Delphi Waring 的信息
- js导入插件注意事项.txt
- Python之路:爬虫之urllib库的基本使用和高级使用
- [实战演练]python3使用requests模块爬取页面内容
- html2canvas不能识别svg的解决方案
- python之路--day8---day9--两日内容
- HTTP协议详解(二)
- 视频剪辑软件原型-videocut
- RHEL7 下双网卡绑定做主备(冗余)
- Redis初探-Redis安装
- Qt程序ibus输入法不跟随
- JDK8 - Function介绍
- Android oncreate onupgrade什么时候被调用
- Ajax-java中的ajax使用,以及编码问题
- 使用GDI技术创建ASP.NET验证码
- 《Pro Git》第3章 分支
热门文章
- python 图形
- MAC 上抓取网页数据的工具有哪些?
- Spark(四十六):Spark 内存管理之—OFF_HEAP
- 解决request.getSession().getServletContext().getRealPath(";/";)为null问题
- 脸型分类-Face shape classification using Inception v3
- 漏洞分析:OpenSSH用户枚举漏洞(CVE-2018-15473)分析
- JS数组常见方法的深浅拷贝分类
- SpringMvc使用JavaMail发送邮件,并使用465端口开启ssl加密传输,保证邮件安全
- markdown如何在表格内换行?
- AndoridSQLite数据库开发基础教程(4)