-5

I'm planning on setting up a VPS "service" just for a couple of my friends to use, but I only have one IP. I'd like to set up nginx so it works like this:

domain1 . com -> 192.168.1.1

domain2 . com -> 192.168.1.2

It was mentioned here so I know it's possible somehow.

Also, assuming this is all set up and working, would I be able to go to domain1 .com:400 and access 192.168.1.1 port 400?

Thanks!

Nick
  • 1
  • 1
  • `Also, assuming this is all set up and working, would I be able to go to domain1 .com:400 and access 192.168.1.1 port 400?` no. – AD7six May 25 '15 at 07:10
  • do you want to run a different service on 400? If 400 isn't for accessing the site then simply port forward 400 to 192.168.1.1 – Drifter104 May 25 '15 at 08:50
  • 400 was an example, the goal here is to make all ports accessible. – Nick May 25 '15 at 15:12
  • @AD7six is there any way to get that working? – Nick May 25 '15 at 16:07
  • with two public ips. Or just have you friends each rent a digital ocean droplet each. Either way though, nothing really to do with nginx, and off topic as this isn't related to **managing information technology systems in a business environment.** – AD7six May 25 '15 at 19:50
  • Make sure you get a VPS with IPv6. That way you'll have enough addresses to allocate one to each site. That's going to make things easier for you on the long term. (And there are so many VPS providers to choose from, that restricting yourself to only those with IPv6 support is going to make the choice easier.) – kasperd May 26 '15 at 11:45
  • @AD7six The exact same question applies to professional deployments. I'm sure this question must have multiple duplicates and at least one of those has got to be stated much more clearly. – kasperd May 26 '15 at 11:50
  • @kasperd maybe this question: [Dynamic port forwarding based on hostname](http://serverfault.com/questions/255055/dynamic-port-forwarding-based-on-hostname-or-originating-ip), of most relevance `At the IP level, there is no such thing as a hostname`. Note specifically this statement by the op `the goal here is to make all ports accessible` - Since the solution to that is having more than one public ip (and using ip tables) - I don't see how this has anything to do with nginx per se. Happy to be proven/demonstrated wrong though. – AD7six May 26 '15 at 11:57
  • 1
    @AD7six Linux supports having a socket listen on all port numbers by using an iptables rule and a socket option. Proxying TCP connections to backends based on hostname is possible for all protocols in which the client speaks first and the client send the hostname in clear before waiting for data from the server. I know of two protocols satisfying that requirement, those are HTTP and HTTPS. I have a PoC patch adding the same to SSH. Such a proxy can also detect which protocol the client is speaking in order to find the hostname, so you can run multiple protocols on the same port. – kasperd May 26 '15 at 12:35
  • @AD7six I have no idea whether nginx supports such a thing. I have written such a proxy myself though. My proxy is specifically intended to provide an IPv4 frontend for IPv6-only backends. – kasperd May 26 '15 at 12:37

2 Answers2

2

You might try setting up multiple server instances, each responding to the appropriate server_name and then proxying from each one as follows:

server {
    listen x.x.x.x:400; # your one IP

    server_name domain1.com;

    location / {
        proxy_pass http://192.168.1.1;
    }
}

server {
    listen x.x.x.x:400: # your one IP

    server_name domain2.com;

    location / {
        proxy_pass http://192.168.1.2;
    }
}
greg
  • 129
  • 6
  • Is there a way to make it listen on more ports than just 80 and 400? Or do I have to write a rule for each port I want to access? – Nick May 25 '15 at 15:14
  • You can add more `listen` statements. This is how you would listen to say 80 and 443 in the same server instance. – greg May 25 '15 at 21:11
0

Something like following will work for you:

    map $http_host $upstream_proxy {
            default     192.168.1.3;

            domain1.com 192.168.1.1;
            domain2.com 192.168.1.2;
    }

    server {
            server_name _;

            location / {
                    proxy_pass http://$upstream_proxy;
            }       
    }

For more information, refer to nginx_http_map_module.

HTH

abbe
  • 356
  • 1
  • 12
  • For testing, I have a DNS server set up to redirect the domain1 and domain2 to the servers ip, which would be 192.168.1.3. Instead of directing to the VPS, typing in domain1.com brings me to the nginx page, instead of 192.168.1.1. – Nick May 25 '15 at 14:57