John Zhang
John Zhang

Reputation: 518

How can I configure Nginx to forward www.myserver.com/XYZ to different servers depending on XYZ?

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.

Upvotes: 1

Views: 2645

Answers (2)

Dayo
Dayo

Reputation: 12775

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

Upvotes: 5

Tiago Albuquerque
Tiago Albuquerque

Reputation: 1

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.

Upvotes: -1

Related Questions