Nginx代理传递子域,节点vhost

Nginx proxy pass subdomain, node vhost

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

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

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

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

有了这个 :

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

Express app 运行 on localhost:9000 handle request /mysubdomainname,这意味着正则表达式是好的。

但是当我尝试时:

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

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

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

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

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

nginx.conf

http {

upstream express {
  server localhost:3000;
}

domain.com 里面 nginx/sites-available

server {

listen 80;
server_name ~^(?<subdomain>.+)\.domain\.com$;

location / {
   proxy_set_header Subdomain $subdomain;
   proxy_set_header Host $host;
   proxy_pass http://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!');
});