修改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. 手写HashMap实践

    1.什么是HashMap 2.源码分析 3.手写实现 4.不足 一.什么是HashMap hash散列 将一个任意长度通过某种算法(hash函数算法)换成一个固定值 map: 地图x,y 存储 总结: ...

  2. 卸载node和npm

    sudo npm uninstall npm -g yum remove nodejs npm -y

  3. Java基础 Scanner 使用nextLine接收字符串

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  4. vue的vuex在使用...mapState 和...mapGetter报错的解决方案

    from:https://blog.csdn.net/ysterling/article/details/88145765 采用mapState 和mapGetter,在页面使用时报错,这是因为bab ...

  5. flutter 数据存储 SP和sqlite

    添加插件: shared_preferences: ^0.4.2 path_provider: ^1.2.0 sqflite: ^0.12.0 import 'dart:async'; import ...

  6. flutter 不规则底部工具栏实现

    import 'package:flutter/material.dart'; import 'each_view.dart'; class BottomAppBarDemo extends Stat ...

  7. openresty开发系列19--lua的table操作

    openresty开发系列19--lua的table操作 Lua中table内部实际采用哈希表和数组分别保存键值对.普通值:下标从1开始 不推荐混合使用这两种赋值方式. local color={fi ...

  8. spark sql插入表时的文件个数研究

    spark sql执行insert overwrite table时,写到新表或者新分区的文件个数,有可能是200个,也有可能是任意个,为什么会有这种差别? 首先看一下spark sql执行inser ...

  9. 搭建Keepalived+LVS-DR集群

    (1).Keepalived概述 keepalived 是一个类似于 layer3, 4 & 5 交换机制的软件,也就是我们平时说的第 3 层.第 4 层和第 5层交换. Keepalived ...

  10. java 时间的原生操作和工具类操作

    package com.xc.test.dateoperation; import org.apache.commons.lang3.time.DateFormatUtils; import org. ...