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的不同项目.整合完毕后发现其 ...
随机推荐
- web通信 长连接、长轮询
http://www.cnblogs.com/hoojo/p/longPolling_comet_jquery_iframe_ajax.html
- Winform 遍历 ListBox中的所有项
foreach(DataRowView row in listBox.Items ) { MessageBox.Show(row["displayMember"].ToString ...
- JAVA常见算法题(五)
package com.xiaowu.demo; /** * 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. * * * @ ...
- ncurses简单的一个多窗体程序
#include <ncurses.h> #include <string.h> #include <iostream> #include <stdlib.h ...
- this的四种绑定规则总结
一.默认绑定 1.全局环境中,this默认绑定到window 2.函数独立调用时,this默认绑定到window console.log(this === window);//true functio ...
- Python从2.7.5升级到3.6.5
Python从2.7.5升级到3.6.5学习了:https://blog.csdn.net/liang19890820/article/details/51079633 wget https://ww ...
- Eclipse对于多个Java项目的支持并不友好!
本文吐槽! 如果我们创建两个Java项目.一个叫StatsReader.把数据从网上下载到本地数据库里.一个叫StatsViewer.把数据从数据库里拿出来呈现给用户.这两个项目都要用同一个外部类库m ...
- highCharts怎样实现json数组数据的图形展示
昨天花了一天的时间学习了一下highcharts.主要的内容差点儿相同都看了一遍,然后试着写了一个完整的demo,期间可谓百转千回.费了不少功夫.终于还是实现了我所想要的效果图,接下来我将怎样实现统计 ...
- dr-helper项目设计介绍(一个包括移动端和Web端的点餐管理系统)
一.源代码路径 https://github.com/weiganyi/dr-helper 二.界面 通过浏览器訪问Web服务,能够看到界面例如以下: ADT-Bundle编译project生成dr- ...
- Dungeon Master ZOJ 1940【优先队列+广搜】
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...