nginx的proxy_pass到$host的问题
今天在配置一个location的时候,希望使用一个变量如$host来指示nginx代理:
- location /test/
- {
- proxy_pass http://$host;
- }
如你想不到,这个配置是不能使用的,查看error.log,打出来的信息也无法帮助解决问题。
但相同情况下,root标签就工作得很好:
- locatin /test/
- {
- root /dev/shm/$host;
- }
令人匪夷所思,估计这是nginx的一个bug,或者是一个搅不清的逻辑?
把上面的错误配置改成
- location /test/
- {
- proxy_pass http://$host/;
- }
或者
- set $vhost "test.sudone.com";
- location /test/
- {
- proxy_pass http://$vhost;
- }
等等,都不能解决问题,最后是用了个奇怪的配置方式,居然能工作……
- location /test/ {
- proxy_pass http://127.0.0.1:80;
- proxy_set_header Host $host;
- }
这个配置就能工作正常,但上面这个配置只支持单ip的后端,能不能支持upstream呢?
再改一下测测
- upstream backend.sudone.com
- {
- server 127.0.0.1:80;
- }
- location /test/
- {
- proxy_pass http://backend.sudone.com;#固定写的后端upstream
- proxy_set_header Host $host;#$host=test.sudone.com;
- }
嗯?能用!
有了这个办法,我便不用在数十个主机配置内把/test/这个location贴上数十次,另外,它也可以支持这样的写法:
- location /test/
- {
- proxy_pass http://127.0.0.1:80;
- proxy_set_header Host fetch.$host;
- }
还可以用正则表达式对host进行匹配:
- set $vhost $host;
- if ($host ~* "([^\.]+)\.92csz\.com")
- {
- set $vhost $1.92csz.com;
- }
- location /test/
- {
- proxy_pass http://127.0.0.1:80;
- proxy_set_header Host fetch.$host;
- }
可用的方面就广了。
---------------------------------------------------
补充:
在这个配置里,我的nginx代理的后端(backend)也是nginx,目前尚不清楚其它的web服务器如apache等是否能在这样的配置下工作正常。
nginx的proxy_pass到$host的问题的更多相关文章
- Nginx配置proxy_pass转发的/路径问题
Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...
- Nginx的proxy_pass及upstream的小型负载均衡
proxy_pass Nginx的proxy_pass将请求代理到其他的后端服务器.例如 listen 9999; server_name wyc.com; location /test/aaa { ...
- nginx中proxy_pass小斜杠
nginx中proxy_pass小斜杠 1. 故事背景 相信做微信公众号开发的朋友都知道,要想在微信中预览效果,必须使用域名访问.很多朋友使用内网穿透工具.不仅不好用还不稳定.所以,发挥脸厚吃天下的态 ...
- 记录一次 Nginx 配置 proxy_pass 后 返回404问题
一. Nginx 配置 proxy_pass 后 返回404问题 故障解决和定位 1.1. 问题 在一次生产涉及多次转发的配置中, 需求是下面的图: 在配置好了 proxy_pass 之后,请求 ww ...
- Nginx配置proxy_pass
nginx配置proxy_pass,需要注意转发的路径配置 1.location /test/ { proxy_pass http://t6:8300; } 2.location /test/ { p ...
- nginx 设置默认虚拟 host
nginx 设置默认虚拟 host listren 80 default_server
- nginx 之 proxy_pass
nginx中有两个模块都有proxy_pass指令 ngx_http_proxy_module的proxy_pass 语法: proxy_pass URL; 场景: location, if in l ...
- 解决nginx使用proxy_pass反向代理时,cookie丢失的问题
1. 如果只是host.端口转换,则cookie不会丢失.例如: location /project { proxy_pass http://127.0.0.1:8080/pr ...
- 解决nginx中proxy_pass到tomcat的session丢失问题
之前在配置tomcat的时候都是一个项目对应一个tomcat,也就是一个端口.最近需要把两个项目整合到同一个tomcat中,通过配置nginx让两个域名指向同一tomcat的不同项目.整合完毕后发现其 ...
随机推荐
- rsync + inotify 同步
1. 配置rysnc server:同步机,同步被同步机更新的文件,很多台vi /etc/rsyncd.conf uid=rootgid=rootuse chroot=nomax connection ...
- Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)
转载自https://github.com/pzxwhc/MineKnowContainer/issues/25 包括:1. 栈溢出(StackOverflowError)2. 堆溢出(OutOfMe ...
- [JZOJ3105]拼图
题目大意: 给你一个起始串$a(|a|\leq 300)$,一个目标串$b(|b|\leq 300)$,以及$n(n\leq 8)$个小串$s_0,s_2,\ldots,s_{n-1}(|s_i|\ ...
- Java字符串(String)
从表面上看,字符串就是双引号之间的数据,例如“微学苑”.“http://www.weixueyuan.net”等.在Java中,可以使用下面的方法定义字符串: String stringName ...
- 彻底理解H5的DOM事件
我们大家都知道,人与人之间的交流可以通过语言,文字,肢体动作,面部微表情等,但是你知道Javascript和HTML之间是通过什么进行交互的么?你又知道Javascript和HTML之间是如何进行交互 ...
- App Distribution Guide (一)
This guide contains everything you need to know to distribute an app through the App Store or Mac Ap ...
- CSS的7种常用的垂直居中的方法
1.绝对定位上下百分之五十然后上外边距做外边距都是他的宽高的一半 #child{ width: 200px; height: 150px; position: absolute; left: 50%; ...
- 利用jquery.form.js实现将form提交转为ajax方式提交的方法(带上传的表单提交)
提供一种方法就是利用jquery.form.js. (1)这个框架集合form提交.验证.上传的功能. 核心方法 -- ajaxForm() 和 ajaxSubmit() $('#myForm').a ...
- 用android连小米手机做profiling
安装失败 Installation failed with message Failed to finalize session http://www.jianshu.com/p/fea7d96385 ...
- 为集群配置Impala和Mapreduce
FROM: http://www.importnew.com/5881.html -- 扫描加关注,微信号: importnew -- 原文链接: Cloudera 翻译: ImportNew.com ...