今天在配置一个location的时候,希望使用一个变量如$host来指示nginx代理:

  1. location /test/
  2. {
  3. proxy_pass http://$host;
  4. }

如你想不到,这个配置是不能使用的,查看error.log,打出来的信息也无法帮助解决问题。
但相同情况下,root标签就工作得很好:

  1. locatin /test/
  2. {
  3. root /dev/shm/$host;
  4. }

令人匪夷所思,估计这是nginx的一个bug,或者是一个搅不清的逻辑?
把上面的错误配置改成

  1. location /test/
  2. {
  3. proxy_pass http://$host/;
  4. }

或者

  1. set $vhost "test.sudone.com";
  2. location /test/
  3. {
  4. proxy_pass http://$vhost;
  5. }

等等,都不能解决问题,最后是用了个奇怪的配置方式,居然能工作……

  1. location /test/ {
  2. proxy_pass http://127.0.0.1:80;
  3. proxy_set_header Host $host;
  4. }

这个配置就能工作正常,但上面这个配置只支持单ip的后端,能不能支持upstream呢?
再改一下测测

  1. upstream backend.sudone.com
  2. {
  3. server 127.0.0.1:80;
  4. }
  1. location /test/
  2. {
  3. proxy_pass http://backend.sudone.com;#固定写的后端upstream
  4. proxy_set_header Host $host;#$host=test.sudone.com;
  5. }

嗯?能用!
有了这个办法,我便不用在数十个主机配置内把/test/这个location贴上数十次,另外,它也可以支持这样的写法:

  1. location /test/
  2. {
  3. proxy_pass http://127.0.0.1:80;
  4. proxy_set_header Host fetch.$host;
  5. }

还可以用正则表达式对host进行匹配:

  1. set $vhost $host;
  2. if ($host ~* "([^\.]+)\.92csz\.com")
  3. {
  4. set $vhost $1.92csz.com;
  5. }
  6. location /test/
  7. {
  8. proxy_pass http://127.0.0.1:80;
  9. proxy_set_header Host fetch.$host;
  10. }

可用的方面就广了。

---------------------------------------------------

补充:

在这个配置里,我的nginx代理的后端(backend)也是nginx,目前尚不清楚其它的web服务器如apache等是否能在这样的配置下工作正常。

nginx的proxy_pass到$host的问题的更多相关文章

  1. Nginx配置proxy_pass转发的/路径问题

    Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...

  2. Nginx的proxy_pass及upstream的小型负载均衡

    proxy_pass Nginx的proxy_pass将请求代理到其他的后端服务器.例如 listen 9999; server_name wyc.com; location /test/aaa { ...

  3. nginx中proxy_pass小斜杠

    nginx中proxy_pass小斜杠 1. 故事背景 相信做微信公众号开发的朋友都知道,要想在微信中预览效果,必须使用域名访问.很多朋友使用内网穿透工具.不仅不好用还不稳定.所以,发挥脸厚吃天下的态 ...

  4. 记录一次 Nginx 配置 proxy_pass 后 返回404问题

    一. Nginx 配置 proxy_pass 后 返回404问题 故障解决和定位 1.1. 问题 在一次生产涉及多次转发的配置中, 需求是下面的图: 在配置好了 proxy_pass 之后,请求 ww ...

  5. Nginx配置proxy_pass

    nginx配置proxy_pass,需要注意转发的路径配置 1.location /test/ { proxy_pass http://t6:8300; } 2.location /test/ { p ...

  6. nginx 设置默认虚拟 host

    nginx 设置默认虚拟 host listren 80 default_server

  7. nginx 之 proxy_pass

    nginx中有两个模块都有proxy_pass指令 ngx_http_proxy_module的proxy_pass 语法: proxy_pass URL; 场景: location, if in l ...

  8. 解决nginx使用proxy_pass反向代理时,cookie丢失的问题

    1. 如果只是host.端口转换,则cookie不会丢失.例如:    location /project {        proxy_pass   http://127.0.0.1:8080/pr ...

  9. 解决nginx中proxy_pass到tomcat的session丢失问题

    之前在配置tomcat的时候都是一个项目对应一个tomcat,也就是一个端口.最近需要把两个项目整合到同一个tomcat中,通过配置nginx让两个域名指向同一tomcat的不同项目.整合完毕后发现其 ...

随机推荐

  1. JSK 18: 跳跃游戏

    题目描述 给定一个非负整数数组,假定你的初始位置为数组第一个下标. 数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如:$A = [2,3,1,1, ...

  2. log4j - 1

    具体内容: 1.       如何在项目中配置log4j使得该系统可以输出web test的日志文件(自定义格式)到工程dist目录下的junitLog/WebTestLog.log目录下,输出508 ...

  3. JAVA RandomAccessFile writeBytes

    writeBytes public final void writeBytes(String s) throws IOException 按字节序列将该字符串写入该文件.该字符串中的每个字符均按顺序写 ...

  4. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  5. Map泛型集合-国家中文和英文的键值映射

    package collection; import java.util.HashMap; import java.util.Map; public class Test5 { public stat ...

  6. zookeeper 学习笔记3

    ZooKeeper 允许客户端向服务端注册一个 Watcher 监听, ZooKeeper 允许客户端向服务端注册一个 Watcher 监听, ZooKeeper 允许客户端向服务端注册一个 Watc ...

  7. sqlmap工具命令行的含义

    SQLMAP参数介绍 sqlmap的使用方式:python sqlmap.py [options]: sqlmap中一共有以下十六个选项卡: 帮助选项卡: Target(目标选项卡): Request ...

  8. ylbtech-KeFuYunWei(服务运维考核系统)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-KeFuYunWei(服务运维考核系统)-数据库设计 DatabaseName:KEFUYUNWEI Model:Admin 用户后台管理 ...

  9. Linux Ubuntu下Dropbox图标消失

    Linux下的Dropbox是支持命令行模式的.,在terminal中输入dropbox后,如下提示: Dropbox command-line interface commands: Note: u ...

  10. HNU11376:Golf Bot

    Problem description Input The first line has one integer: N, the number of different distances the G ...