0

I am trying to figure things out and I am confused. I know some things but not enough.

I know that domains actually point to the IP address of a server. So if I want to have two apps, they both have to point to the same server. For example:

domain1.com => 139.130.4.5

domain2.com => 139.130.4.5

And I would have to configure both of them to point to the same IP address? Right?

They both will default to port 80 right (139.130.4.5:80)? How does the server know which app to serve?

Lets say I have these two apps on Debian:

127.0.0.1:3000

127.0.0.1:4000

And NGINX accepting request on port 80 and somehow deciding which up to serve. How does it all work?

What I was thinking first, you would have to configure domains to point to:

domain1.com => 139.130.4.5:3000

domain2.com => 139.130.4.5:4000

But it doesn't work like that, if not like that, how? :)

Thanks!

3 Answers 3

0

It's called name based virtual hosts. Nginx would serve the site based upon the FQDN requested.

2
  • ok, so its all based on a domain name, you add matching server_name example.com; and magic just happens? :) Jan 22, 2016 at 23:13
  • I don't know nginx like I know apache. In apache you create name based virtual host entries and apache will watch the request headers. If sitea.com is requested, it will deliver content based upon the specification in the virtual host configuration. No magic.
    – Linuxx
    Jan 22, 2016 at 23:30
3

This can most certainly be done utilising Virtual Hosts. Domain1.com could use the original web root folder and vhost file. If you do not wish to do this, you can repeat the steps below for domain1 as well as 2.`

Steps (ubuntu 14.04):

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

sudo nano /etc/nginx/sites-available/domain2.com
  • Uncomment listen 80, so traffic coming through will be connected

  • Change the web root

  • Change the domain name to match your other domain

Next, create a link from the vhost available to the ones enabled.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Restart nginx

sudo service nginx restart

With this, one IP can serve many different websites, almost all shared web hosting providers do perform this practice due to the shortage of IPv4 space and the costly deployment of V6. The server will know which know which app to serve to the client from the following block:

server_name example.com;

As for your other suggestion, yes, this can be done.

An example is shown below

server {
     listen   80; ## IPv4, change this port!

I am no nginx expert (Apache lover), I hope this has helped

2
  • As a fellow apache lover, this is an excellent explanation. +1. These kids and their nginx need to get off my lawn!
    – Linuxx
    Jan 22, 2016 at 23:33
  • Hahah thanks, I hope it works out for youbetternot, I think everyone has their "I want to use nginx phase" Jan 22, 2016 at 23:35
-1

I followed this protocol from scratch, on freshly installed nginx. and it worked for me. https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts

1
  • Links to external resources are encouraged, but the salient information from the link should be included in the post. It’s best for answers to be as self-contained as possible. Always quote the most relevant part of the external resource as its URL may change or go permanently offline. See How do I write a good answer? Jun 2, 2016 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .