如何配置 Nginx 根据 XYZ 将 www.myserver.com/XYZ 转发到不同的服务器?

发布于 2025-01-06 17:34:16 字数 183 浏览 0 评论 0原文

假设我的机器上有多个不同的 Tornado 服务器。我希望根据 URL 调用它们。我如何配置 Nginx 来做到这一点?例如,我在 localhost:8000 上有服务器 A,在 localhost:9000 上有服务器 B。我希望 A 处理对 www.myserver.com/A 的请求,B 处理对 www.myserver.com/B 的请求。

Suppose I have multiple different Tornado servers on my machine. I would like them to be called depending on the URL. How can I configure Nginx to do this? E.g., I have servers A on localhost:8000 and B on localhost:9000. I would like A to handle requests to www.myserver.com/A and B to handle requests to www.myserver.com/B.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2025-01-13 17:34:16

您是否尝试过类似的东西...

server {
    listen 80;
    server_name example.com;
    root /path/to/webroot;

    location / {
        # For requests to www.myserver.com/A
        location ~ ^/A {
            proxy_pass localhost:8000;
        }
        # For requests to www.myserver.com/B
        location ~ ^/B {
            proxy_pass localhost:9000;
        }
        # Some will skip the "A" or "B" flags ... so handle these
        proxy_pass localhost:9000$request_uri;
    }

这可以扩展/细化为类似...的东西。

    location / {
        # For requests to www.myserver.com/A/some/request/string
        location ~ ^/A(.+)$ {
            proxy_pass localhost:8000$1;
        }
        # For requests to www.myserver.com/B/some/request/string
        location ~ ^/B(.+)$ {
            proxy_pass localhost:9000$1;
        }
        # Some will skip the "A" or "B" flags ... so handle these
        proxy_pass localhost:9000$request_uri;
    }

也许更好的方法是捕获一台服务器的请求并将其余的默认到另一台服务器....

    location / {
        # For requests to www.myserver.com/A/some/request/string
        location ~ ^/A(.+)$ {
            proxy_pass localhost:8000$1;
        }
        # Send all other requests to alternate location.
        # Handle both well formed and not well formed ones.
        location ~ ^/(.)/(.+)$ {
            proxy_pass localhost:9000/$1;
        }
        proxy_pass localhost:9000$request_uri;
    }
}

Have you tried something like ...

server {
    listen 80;
    server_name example.com;
    root /path/to/webroot;

    location / {
        # For requests to www.myserver.com/A
        location ~ ^/A {
            proxy_pass localhost:8000;
        }
        # For requests to www.myserver.com/B
        location ~ ^/B {
            proxy_pass localhost:9000;
        }
        # Some will skip the "A" or "B" flags ... so handle these
        proxy_pass localhost:9000$request_uri;
    }

This can be expanded / refined into something like ....

    location / {
        # For requests to www.myserver.com/A/some/request/string
        location ~ ^/A(.+)$ {
            proxy_pass localhost:8000$1;
        }
        # For requests to www.myserver.com/B/some/request/string
        location ~ ^/B(.+)$ {
            proxy_pass localhost:9000$1;
        }
        # Some will skip the "A" or "B" flags ... so handle these
        proxy_pass localhost:9000$request_uri;
    }

A better way perhaps is to catch requests for one server and default the rest to the other ....

    location / {
        # For requests to www.myserver.com/A/some/request/string
        location ~ ^/A(.+)$ {
            proxy_pass localhost:8000$1;
        }
        # Send all other requests to alternate location.
        # Handle both well formed and not well formed ones.
        location ~ ^/(.)/(.+)$ {
            proxy_pass localhost:9000/$1;
        }
        proxy_pass localhost:9000$request_uri;
    }
}
数理化全能战士 2025-01-13 17:34:16

我不相信这是可能的,你无法配置对文件夹的 dns 请求,如果你创建一个文件夹 /xyz,你可以创建一个框架集来打开 localhost:9000

但如果你真的想达到预期的结果,我建议你使用子域。

i dont believe its possible, u cant configure the dns requests to folders, if u create a folder /xyz u can create a frameset to open the localhost:9000

But if u really want reach the desired results i advice you to use subdomains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文