如何将多个 node.js 实例加入到单个域?

如何将多个 node.js 实例加入到单个域?

我在同一个 IP 上有几个 node.js 实例。每个实例通过不同的域运行nginx反向代理。

例如:

map_hash_bucket_size 128;

map $host $backend_servers {
  hostnames;
  default       127.0.0.1:3000;

  example.com   127.0.0.1:3001;
  example.net   127.0.0.1:3002;
  example.org   127.0.0.1:3003;
}

server {
    listen 1.2.3.4:80 default_server;
    server_name  _;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://$backend_servers; # upstream here
        proxy_redirect off;
    }
}

example.com我想将所有带有路径前缀的流量引导/foo到服务器并从服务器中127.0.0.1:3002删除。example.net

旧链接example.net/bar应重定向至状态301 永久移动example.com/foo/bar

这是我的最终目标:

example.com      >> 127.0.0.1:3001
example.com/foo  >> 127.0.0.1:3002
example.net      >> example.com/foo  # 301 redirect
example.org      >> 127.0.0.1:3003   # no change

有什么建议么?

答案1

您可以像这样修改 nginx 代理服务器配置:

server {
    server_name example.com;
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:3001;
                }
    location /foo {
        proxy_pass http://127.0.0.1:3002;
    }
} 
server {
    server_name example.org;
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:3003;
    }
}
server {
    server_name example.net;
    listen 80;
    return 301 http://example.com/foo;
}

相关内容