【问题标题】:Nginx proxy pass subdomain, node vhostNginx代理传子域,节点vhost
【发布时间】:2016-12-26 11:17:07
【问题描述】:

我在 localhost 子域上的 nginx proxy_pass 重定向遇到了一些问题。我有一个域“domain.com”,我想在 *.localhost:9000 上重定向 *.domain.com 上的所有请求。然后节点处理 *.localhost:9000 上的所有请求到好的 express 应用程序。

当我尝试以下操作时在 nginx conf 上:

server {
    server_name extranet.domain.com;
    listen 80;
    location / {
       proxy_pass http://extranet.localhost:9000;
    }
}

extranet.domain.com 上的请求被很好地重定向到良好的 express webapp。

有了这个:

server {
    server_name ~^(.*?)\.domain\.com$;
    listen 80;
    location / {
       proxy_pass http://localhost:9000/$1;
    }
}

localhost:9000 上运行的Express 应用程序处理请求/mysubdomainname,这意味着正则表达式是好的。

但是当我尝试时:

server {
    server_name ~^(.*?)\.domain\.com$;
    listen 80;
    location / {
       proxy_pass http://$1.localhost:9000;
    }
}

*.domain.com 上的所有请求都返回 http 代码 502。 为什么 http://localhost:9000/$1; 有效而不是 http://$1.localhost:9000; ? (所有子域都设置在/etc/hosts)。

提前致谢。我完全迷路了!

【问题讨论】:

    标签: node.js nginx subdomain


    【解决方案1】:

    当主机名在运行时未知时,nginx 必须使用其own resolver。与 OS 提供的解析器不同,它不使用您的 /etc/hosts 文件。

    【讨论】:

    • 谢谢,所以我想要做的唯一方法是添加 server { server_name : subdomain1.domain.com;听:80;位置 / { proxy_pass subdomain1.localhost:9000; } } 每个子域?
    • 或者配置自己的本地dns服务器。
    【解决方案2】:

    也许这会给你一个提示,我想将子域从 Nginx 传递到 Express 应用程序。这是我的代码:

    nginx.conf

    http {
    
    upstream express {
      server localhost:3000;
    }
    

    在 nginx/sites-available 中的 domain.com

    server {
    
    listen 80;
    server_name ~^(?<subdomain>.+)\.domain\.com$;
    
    location / {
       proxy_set_header Subdomain $subdomain;
       proxy_set_header Host $host;
       proxy_pass http://express;
     }
    }
    

    Express 应用 index.js

    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
        const subdomain = req.headers.subdomain;
    });
    
    app.listen(3000, function () {
      console.log('Example app listening on port 4000!');
    });
    

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 2023-03-31
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2016-07-29
      • 1970-01-01
      • 2022-11-13
      相关资源
      最近更新 更多