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

时间:2012-02-18 18:33:36

标签: nginx

假设我的计算机上有多个不同的Tornado服务器。我希望根据URL调用它们。如何配置Nginx来执行此操作?例如,我在localhost:8000上有服务器A,在localhost:9000上有B.我希望A处理对www.myserver.com/A和B的请求,以处理对www.myserver.com/B的请求。

2 个答案:

答案 0 :(得分:5)

你有没有试过像......

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;
    }
}

答案 1 :(得分:-1)

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

但是如果你真的希望达到预期的效果,我建议你使用子域名。