转载自: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;
}
}
}

最新文章

  1. BASH_SUBSHELL 变量不生效的情况
  2. 解决windows下FileZilla server中文乱码问题
  3. CSS样式表 选择器
  4. 没有好看的 Terminal 怎么能够快乐地写代码
  5. python unicode字节串转成中文问题
  6. css笔记——小图标文字对齐中级解决方案
  7. Java File类读取文件属性
  8. Eclipse 代码提示不显示的问题
  9. mysql 常见语句
  10. 一个二维码-->网址-->iOS/Android跳转
  11. net view ERROR 6118
  12. 找几张图片制作GIF
  13. 使用DatagramSocket和DatagramPacket进行简单的通信
  14. JS创建对象,数组,函数的三种方式
  15. OpenCV Mat与UIImage之间的转换
  16. tmux的使用
  17. iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮
  18. 使用newScheduledThreadPool来模拟心跳机制
  19. vue+element ui 的表格列使用组件
  20. Android四大组件-服务

热门文章

  1. AndroidStudio报错:GradleSyncIssues-Could not install Gradle distribution from...
  2. 架构师成长系列 | 从 2019 到 2020,Apache Dubbo 年度回顾与总结
  3. C#中FolderBrowserDialog类打开文件夹使用说明
  4. .Net框架的模块代码生成器--其二(dotnet tool)
  5. MySQL 的一条语句是怎么执行的
  6. element-ui 和ivew-ui的table导出export纯前端(可用)
  7. 剑指offer-面试题39-数组中出现次数超过一半的数字-快速排序
  8. maven的核心概念——坐标
  9. vim 配置 jedi-vim( ubuntu:15.10 )
  10. 理解Android线程创建流程