Ubuntu – Multiple websites on nginx, one IP

nginx

So I'm using digital ocean and nginx. I want to host multiple websites (my projects) but dont want to buy a domain for each one.

Is there a way to host multiple websites on nginx and digital ocean while accessing them using that one ip?

Best Answer

There's two ways to achieve this. Either you do everything by IP address, with subfolder locations, or you will need to buy one domain and then have multiple subdomains on that domain (subdomains shouldn't cost anything, if you purchase the domain, but check with your registrar).

I don't advise the IP-address method if only because it's very evil to have to remember IP addresses, and if you try sharing information to others, you also have the same problem of others having to remember the IP address.


The one-IP, many subfolders approach, without a domain name

NOTICE! We don't have any information about your projects you are working on. We need to know more to determine whether you can do this approach, as many web frameworks will not work without a true domain name tied to it.


WARNING: In ongoing testing of these examples, it was discovered that the "One domain, many subdirectories" approach does not take kindly to reverse-proxying data to the backend, as the requested URI will include the subdirectories within the URI; this might make backend servers have problems behaving properly.

On the nginx side, we need to do an 'evil' approach to this - one IP address, many docroots and subfolder locations. This is a very very evil approach and can cause a lot of problems with some web frameworks.

Assuming a default nginx install as a base from the repositories, then we have to create a site configuration to handle each project subdirectory request. Then we have to symlink it in the right location.

Create /etc/nginx/sites-available/my-projects with the following (use this as a template/guide - it assumes three projects with static HTML and no dynamic web applications in PHP or python or similar, and you can copy the individual location blocks and make new locations accordingly; it also assumes your server IP is 1.2.3.4).

server {
    listen 80 default_server;

    server_name 1.2.3.4;

    location / {
        return 410;  # Default root of site won't exist.
    }

    location /proj1/ {
        alias /var/www/proj1;
        try_files $uri $uri/ =404;

        # any additional configuration for non-static content
    }

    location /proj2/ {
        alias /var/www/proj2;
        try_files $uri $uri/ =404;

        # any additional configuration for non-static content
    }

    location /proj3/ {
        alias /var/www/proj3;
        try_files $uri $uri/ =404;

        # any additional configuration for non-static content
    }
}

Now we replace the default config (remove it) and add ours:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/my-projects /etc/nginx/sites-enabled

And then restart the nginx service:

# If on 14.04, use this:
sudo service nginx restart

# If on 15.10 or newer, use this:
sudo systemctl restart nginx

The one-domain, multiple subdomains approach.

This answer section assumes you have one domain and multiple subdomains therein. If you do not have this, please clarify this in your question

With each nginx server {} block in the configuration, you will need to define the server name, and likely set a fourth server block as a 'catch all' for other requests.

Example: I have three projects, proj1, proj2, proj3. I have a domain called evil-projects.net (NOTE: Doesn't exist really). I want three different subdomains, one for each nginx configuration which will point to one project each. My server resides at 1.2.3.4, and it will serve all sites.

With the above scenario, we have two parts: domains and subdomains, and the server configuration.

(1): DNS Configuration

Set up your DNS at your host such that the following is true with the DNS records:

evil-projects.net  IN A  1.2.3.4
proj1.evil-projects.net  IN A  1.2.3.4
proj2.evil-projects.net  IN A  1.2.3.4
proj3.evil-projects.net  IN A  1.2.3.4

(2): NGINX configuration at server (1.2.3.4)

Now for your nginx configurations. I am assuming you are going to have the default nginx setups and the packages from the Repositories (I'm going to use 14.04 as a base example). We'll have four configuration files put into /etc/nginx/sites-available, first. You may need to use sudo when creating these files, as the folder in question is owned by root.

/etc/nginx/sites-available/catch-all - this will be the 'catch all' for any nonvalid domains. I like returning http error code 410 (GONE).

server {
    listen 80 default_server;

    server_name _;

    return 410;
}

Next, we set up the configuration for your sites/projects. I'm going to assume they're all static files, though. Each of these implies that you have different web directories for each project on the server as well (different 'document roots').

/etc/nginx/sites-available/proj1.evil-projects.net:

server {
    listen 80;

    server_name proj1.evil-projects.net;

    root /var/www/proj1;
    index index.htm index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

/etc/nginx/sites-available/proj2.evil-projects.net:

server {
    listen 80;

    server_name proj2.evil-projects.net;

    root /var/www/proj2;
    index index.htm index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

/etc/nginx/sites-available/proj3.evil-projects.net:

server {
    listen 80;

    server_name proj3.evil-projects.net;

    root /var/www/proj3;
    index index.htm index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

We then have to remove the 'default' configuration from /etc/nginx/sites-enabled, and add our own. Again, sudo is needed here.

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/proj1.evil-projects.net /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/proj2.evil-projects.net /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/proj3.evil-projects.net /etc/nginx/sites-enabled/

And then we restart the nginx process:

# If on 14.04, use this:
sudo service nginx restart

# If on 15.04 or newer, use this:
sudo systemctl restart nginx

Once DNS propagates, the sites will work as they should.

Related Question