转载自:https://www.jianshu.com/p/fd16b3d10752

如果没有特别注意 proxy_set_header 配置,使用 proxy_set_header 可能会引起以下问题:

  1. 丢失需要的 header 信息
  2. 拿到意外的 Host 信息
  3. upstream 中的 keepalive 不能生效

在server{}字段,要么,设置齐全关于常用的proxy_set_header, 要么,

要么就在server{}字段一个都不设置,但是在server{}字段的上级要设置齐全。

有点绕

如果在server{}字段设置了任何的proxy_set_header, 那么上级的proxy_set_header都不会继承,只使用server{}字段上默认和新设置的的而已

server{}字段,系统有两个默认

分别是

proxy_set_header Host $proxy_host;

proxy_set_header Connection close;

官方文档

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
Allows redefining or appending fields to the request header passed to the proxied server. 
The value can contain text, variables, and their combinations.
These directives are inherited from the previous level if and
only if there are no proxy_set_header directives defined on the current level.
By default, only two fields are redefined: proxy_set_header Host $proxy_host;
proxy_set_header Connection close; 允许重新定义或附加字段到传递给代理服务器的请求头。该值可以包含文本、变量及其组合。
当且仅当当前级别上没有定义 proxy_set_header 指令时,这些指令从上级继承。默认情况下,只有两个值被重新定义:

问题的关键

在当前级别的配置中没有定义 proxy_set_header 指令时,这些指令从上级继承。
如果当前级别的配置中已经定义了 proxy_set_header 指令,在上级中定义的 proxy_set_header 指令在当前级别都会失效

举个例子:

这个配置,如果用户访问 example.com/test/index.html,后端服务拿到的 Host 值是 example.com_test,
而不是期望的 example.com;后端服务器会收到 Connection: close 的 Header,
而不能复用连接;后端服务器也不能从 Header 中获取到 X-Real-IP。
http {
...
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr; upstream example.com_test {
server 127.0.0.1:; keepalive ;
} server {
server_name example.com; location ^~ /test/ {
proxy_set_header test test;
proxy_pass http://example.com_test;
}
}
}

注意: 在 location ^~ /test/ {...} 中真正生效的 proxy_set_header 只有这三个

proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;
proxy_set_header test test;

正确的配置

http {
...
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr; upstream example.com_test {
server 127.0.0.1:; keepalive ;
} server {
server_name example.com; location ^~ /test/ {
proxy_set_header test test;
proxy_set_header Host $host;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://example.com_test;
}
}
}

Nginx proxy_set_header 配置注意事项的更多相关文章

  1. nginx代理配置 配置中的静态资源配置,root 和 alias的区别。启动注意事项

    这篇主要内容是:nginx代理配置 配置中的静态资源配置,root 和 alias的区别.启动注意事项! 为什么会在window上配置了nginx呢?最近我们的项目是静态资源单独放在一个工程里面,后端 ...

  2. Nginx 核心配置-自定义日志路径及清空日志注意事项

    Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...

  3. nginx缓存配置的操作记录梳理

    web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...

  4. Windows下Nginx的配置及配置文件部分介绍

    一.在官网下载 nginx的Windows版本,官网下载:http://nginx.org/download/ 选择你自己想要的版本下载,解压 nginx(例如nginx-1.6.3) 包到你的win ...

  5. 使用docker部署nginx并配置https

    我只有一台服务器,但我想在这台服务器上运行多个项目,怎么办? 总不能靠加端口区分吧? 百度和Google是个好东西,于是我找到了答案,使用nginx. 通过nginx,我可以给我的一台服务器配置两个域 ...

  6. spring4+websocket+nginx详细配置

    实现的版本jdk1.7.0_25, tomcat7.0.47.0, Tengine/2.1.1 (nginx/1.6.2), servlet3.0, spring4.2.2 使用maven导入版本3. ...

  7. 理解nginx的配置

    Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...

  8. NGINX下配置404错误页面的方法分享

    NGINX下配置自定义的404页面是可行的,而且很简单,只需如下几步,需要的朋友可以参考下   1. 创建自己的404.html页面 2.更改nginx.conf在http定义区域加入: fastcg ...

  9. nginx的配置总结

    总体而言,nginx的配置比起apache来是要简洁很多,而言容易理解得多的,另外官网的文档也十分的简洁易懂.我们先看一个简化版的配置文件nginx.conf: #user nobody; worke ...

随机推荐

  1. phpstrom laravel代码自动提示

    1.安装composer包 composer require barryvdh/laravel-ide-helper dev-master 2.目录:\config\app.php 的'provide ...

  2. C# 根据天、周、月汇总统计生成统计报表

    先看核心代码: public List<DataEntity> SearchShopSalesReport(DateTimeOffset? dateFrom, DateTimeOffset ...

  3. 安装Matlab R2017a 出现 “弹出DVD1 并插入DVD2” 解决办法超简单

    打开此电脑 找到驱动器虚拟镜像 右击选择弹出 点击另一个文件装载 点击确定即可

  4. jmeter脚本调试过程

    1.添加监听器:查看结果树,再回放脚本 2.权限验证,例如:cookies a.谷歌浏览器F12获取session

  5. if 语句 总结笔记

    1.if 语句 语法: if(condition) statement1; else statement2; graph TD A[JAVA考试] -->|几天后| B(收到成绩单) B --& ...

  6. linux系统下apache服务的启动、停止、重启命令

    本文章简单的介绍了关于linux下在利用命令来操作apache的基本操作如启动.停止.重启等操作,对入门者不错的选择.本文假设你的apahce安装目录为 usr local apache2,这些方法适 ...

  7. Jenkins+robotframework持续集成环境(二)

    配置Jenkins上的robotframework环境 一.添加robot插件 需要导一个robot framework 的包,导包方式如下: 1.进入插件管理页面,选择“可选插件”,在右侧搜索栏搜索 ...

  8. windows下安装openjdk

    redhat版openjdk,解压后就能用,下载地址https://developers.redhat.com/products/openjdk/download. Azul Zulu版openjdk ...

  9. js模拟form提交 导出数据

    //创建模拟提交formfunction dataExport(option) { var form = $("<form method='get'></form>& ...

  10. 解决Mac无法写入U盘问题

    注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/upan-to- ...