Skip to content

Instantly share code, notes, and snippets.

@travelhawk
Last active November 21, 2023 13:25
Show Gist options
  • Save travelhawk/bb49923c3b503cf77c21290af64634e9 to your computer and use it in GitHub Desktop.
Save travelhawk/bb49923c3b503cf77c21290af64634e9 to your computer and use it in GitHub Desktop.
Nginx proxy based on domains

Redirecting Based on Host Name

Let say you want to host example1.com, example2.com, and example3.com on your machine, respectively to localhost:8080, localhost:8181, and localhost:8282.

Note: Since you don't have access to a DNS server, you should add domain name entries to your /etc/hosts (you can't do this on CDF machines):

... 127.0.0.1 example1.com example2.com example3.com ... To proxy eaxmple1.com we can't use the location part of the default server. Instead we need to add another server section with a server_name set to our virtual host (e.g., example1.com, ...), and then a simple location section that tells nginx how to proxy the requests:

server {
    listen       80;
    server_name  example1.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

server {
    listen       80;
    server_name  example2.com;

    location / {
        proxy_pass http://127.0.0.1:8181;
    }
}

server {
    listen       80;
    server_name  example3.com;

    location / {
        proxy_pass http://127.0.0.1:8282;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment