Multiple Websites on Nginx, One Ip
So I'm Using Digital Ocean and Nginx. I Want to Host Multiple Websites (My Projects) but Don't Want to Buy a Domain for Each One. Is There a Way to Host...
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?
1 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