修改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';

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

  1. 将正确的 HTTP 头转发给后端服务器的一些问题

    Apache Software Foundation 的 HTTP 服务器项目(通常称为 Apache)是当今互联网上占据优势的 Web 服务器,它占据了 60% 以上的市场份额.Apache 服务器 ...

  2. Nginx 操作响应头信息的实现

    前置条件:需要编译 ngx_http_headers_module 模块,才支持 header 头信息操作 add_header 意思为将自定义的头信息的添加到响应头,指令为 add_header n ...

  3. 【Nginx】修改响应头,根据不同请求IP重定向到不同IP

    背景: 使用CAS登录的过程中会涉及到三次重定向,如果在同一个局域网内,是没有任何问题的,但如果涉及到跨网访问,这个问题就比较蛋疼了. 解决思路: 通过Nginx对要访问的系统进行代理,根据请求IP来 ...

  4. nginx替换响应头(重点:如何在替换时加上if判断)

    在实现微信小程序内嵌非业务域名时,通过nginx做镜像网站绕过小程序业务域名检测,但有一些表单页面提交后会返回一个302状态,由响应头Location的值决定提交成功后的跳转地址.那么问题来了,这个地 ...

  5. nginx根据目录反向代理到后端服务器

    nginx根据目录反向代理到后端不同的服务器 server {         listen 80;         server_name demo.domain.com;         #通过访 ...

  6. [2014-02-19]如何移除响应头中的.net framework 版本信息 以及mvc版本信息?

    先来看一个简单mvc3网站的响应头 修改Global.asax文件 在Application_Start方法中添加如下代码 MvcHandler.DisableMvcResponseHeader = ...

  7. nginx做反向负载均衡,后端服务器获取真实客户端ip(转)

    首先,在前端nginx上需要做如下配置: location / proxy_set_hearder host                $host; proxy_set_header X-forw ...

  8. nginx做反向负载均衡,后端服务器获取真实客户端ip

    首先,在前端nginx上需要做如下配置: location / proxy_set_header host                   $host; proxy_set_header X-fo ...

  9. Nginx+upstream针对后端服务器容错的运维笔记

    熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...

随机推荐

  1. vmWare安装centos7之后使用yum安装提示there are on enabled repos(修改yum源)

    可以使用这个命令修改yum源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.rep ...

  2. Java获取系统基础参数信息

    一.JVM已经帮我们读取了系统信息 public void testSystemParam(){ Properties p = System.getProperties(); Iterator it ...

  3. 基于ZooKeeper实现简单的服务注册于发现

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Shaun_luotao/article/ ...

  4. 软件定义网络基础---OpenFlow概述

    一:OpenFlow概述 二:交换机模型架构 (一)OpenFlow构架三个组成成分 三:OpenFlow 1.0版本 自OpenFlow1.0发布以来,目前已经有多个版本的OF规范版本被发布 四:O ...

  5. Qt5.10使用了qml后打包发布应用遇到的问题

    现象: 按照https://www.cnblogs.com/judes/p/11247728.html的方法,能生成一些exe所需环境和动态库,但是打开程序,用到了qml的地方,全是黑色的. 原因: ...

  6. Qt编写气体安全管理系统5-数据监控

    一.前言 本项目对设备的监控有四种视图模式,可以任意切换,数据监控.地图监控.设备监控.曲线监控,其中数据监控是最常用的,所以在主界面导航中也排在第一位,综合观察分析了很多气体安全或者组态监控软件,大 ...

  7. matlab基本函数inf, isempty, round, floor, fix

    一起来学演化计算-matlab基本函数inf, isempty, round, floor ,fix 觉得有用的话,欢迎一起讨论相互学习~Follow Me inf matlab中 inf无穷大量+∞ ...

  8. Python - Django - 静态文件相关

    静态文件的路径设置在 settings.py 中 如果该路径发生更改的话,html 中相关路径也要进行修改 CSS: <link href="/static/dashboard.css ...

  9. 【docker 镜像源】解决quay.io和gcr.io国内无法访问的问题

    该问题容易导致image pull back off 错误,应当换源: 微软: https://yeasy.gitbooks.io/docker_practice/install/mirror.htm ...

  10. Java连接MongoDB报错“java.lang.IllegalArgumentException: clusterListener can not be null”的解决办法

    我使用的MongoDB版本是3.6.9. 下面是一个很基础的示例代码,功能就是连接MongoDB: package com.zifeiy.snowflake.handle.etl.mongodb; i ...