Nginx proxy_set_header 配置注意事项
转载自:https://www.jianshu.com/p/fd16b3d10752
如果没有特别注意 proxy_set_header 配置,使用 proxy_set_header 可能会引起以下问题:
- 丢失需要的 header 信息
- 拿到意外的 Host 信息
- 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 配置注意事项的更多相关文章
- nginx代理配置 配置中的静态资源配置,root 和 alias的区别。启动注意事项
这篇主要内容是:nginx代理配置 配置中的静态资源配置,root 和 alias的区别.启动注意事项! 为什么会在window上配置了nginx呢?最近我们的项目是静态资源单独放在一个工程里面,后端 ...
- Nginx 核心配置-自定义日志路径及清空日志注意事项
Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...
- nginx缓存配置的操作记录梳理
web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...
- Windows下Nginx的配置及配置文件部分介绍
一.在官网下载 nginx的Windows版本,官网下载:http://nginx.org/download/ 选择你自己想要的版本下载,解压 nginx(例如nginx-1.6.3) 包到你的win ...
- 使用docker部署nginx并配置https
我只有一台服务器,但我想在这台服务器上运行多个项目,怎么办? 总不能靠加端口区分吧? 百度和Google是个好东西,于是我找到了答案,使用nginx. 通过nginx,我可以给我的一台服务器配置两个域 ...
- 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. ...
- 理解nginx的配置
Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...
- NGINX下配置404错误页面的方法分享
NGINX下配置自定义的404页面是可行的,而且很简单,只需如下几步,需要的朋友可以参考下 1. 创建自己的404.html页面 2.更改nginx.conf在http定义区域加入: fastcg ...
- nginx的配置总结
总体而言,nginx的配置比起apache来是要简洁很多,而言容易理解得多的,另外官网的文档也十分的简洁易懂.我们先看一个简化版的配置文件nginx.conf: #user nobody; worke ...
随机推荐
- PHPExcel使用
参考链接: 官方github:https://github.com/PHPOffice/PHPExcel 设置表格字体颜色等操作:http://www.cnblogs.com/grimm/p/9 ...
- Python 钩子函数详解
###### 钩子函数 ``` import pluggy hookspec = pluggy.HookspecMarker('aaa') hookimpl = pluggy.HookimplMark ...
- Nginx 核心配置
nginx的核心配置在conf/nginx.conf中. 全局配置块 user root; #运行worker进程的账户,user 用户 [组],默认以nobody账户运行 worker_pr ...
- Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)
场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将需要滚动查看的照 ...
- Kakfa集群(2.11-0.10.1.0)版本滚动升级方案
Kafka集群版本升级(2.11-0.10.1.0)升级(2.11-0.10.2.2) 官网升级说明: 一.系统环境Zookeeper集群:172.16.2.10172.16.2.11172.16.2 ...
- Oracle v$session视图显示客户端IP地址
在Oracle数据库中,我们使用session相关视图(v$session.v$active_session_history,dba_hist_active_session_history等)查找问题 ...
- STT-MRAM存在的两个弊端
随着自旋转移矩效应的发现以及材料和结构的优化,基于自旋转移矩效应的STT-MRAM器件应运而生.自从自旋转移矩效应被证实以来,一方面研究人员通过大量的努力尝试降低磁化反转的临界电流,增加热稳定性:另一 ...
- Mybaits(9)MyBatis级联-2
一.鉴别器和一对多级联 1.完善体检表,分为男雇员体检和女雇员体检表 (1)持久层dao编写 package com.xhbjava.dao; import com.xhbjava.domain.Ma ...
- webApi前端ajax调用后端返回{"readyState":0,"status":0,"statusText":"error"}解决方案
var url = data.url, params = data.params, try_times = data.try_times , async = data.sync == 'false' ...
- java.lang.ClassCastException:java.util.LinkedHashMap不能转换为com.testing.models.xxx
后台接收前台的json字符串 转pojo 问题(Object 对应定义的pojo) ObjectMapper mapper=new ObjectMapper(); Object object = ma ...