In this tutorial, you’ll learn how to host multiple websites on a single Nginx web server. I couldn’t find a good tutorial on this topic online, so my goal with this is to teach you how to easily host multiple websites on one Nginx server with one IP address.
On that note, let’s dive right into the tutorial.
1) Create a Document Root for Your Websites
By default, websites are served out of /usr/share/nginx/
, but some Nginx servers use the /var/www/
directory. In either case, create a subdirectory with the name of your domain name. For example, my first domain name is site1.xyz, so my directory is called site1.xzy. Within site1.xyz, create another subdirectory called html.
You can do this with the mkdir -p command like this.
mkdir -p /usr/share/nginx/site1.xyz/html
Repeat this step for additional websites that you want to host on this web server. For this tutorial, my second website is site2.xyz.
mkdir -p /usr/share/nginx/site2.xyz/html
2) Create Your Websites’ Content
To keep things simple, we’ll be creating a basic HTML page called index.html for each of our websites. This is just for demonstration purposes. In reality, your actual website will go here.
First under /usr/share/nginx/site1.xyz/html
, create a file called index.html with the following content.
<!DOCTYPE html> <html> <body> <h1>Welcome to site1.xyz</h1> <p>This is the first website on the Nginx web server</p> </body> </html>
Repeat this step and create a similar file under /usr/share/nginx/site2.xyz/html
.
<!DOCTYPE html> <html> <body> <h1>Welcome to site2.xyz</h1> <p>This is the second website on the Nginx web server</p> </body> </html>
3) Create Nginx Configuration Files
Next, navigate over to the conf.d folder in the nginx directory. Note that sometimes configuration files are located in a sites-available directory.
cd /etc/nginx/conf.d
In here, create a configuration file called site1.conf containing a server block like this. Note that the server_name is the domain name of your website.
server { listen 80; server_name site1.xyz; location / { root /usr/share/nginx/site1.xyz/html; index index.html; } }
Repeat this step for additional websites and update references of your website’s name accordingly. For example, my second configuration file is called site2.conf and looks like this.
server { listen 80; server_name site2.xyz; location / { root /usr/share/nginx/site2.xyz/html; index index.html; } }
4) Restart Nginx to Apply Changes
Now we have to tell Nginx to start hosting our multiple websites, but first let’s make sure there are no syntax errors in your Nginx configuration files.
nginx -t
Next we can restart the Nginx web server in order for our changes to be recognized.
systemctl restart nginx
5) Update DNS Settings
Finally, we must update our DNS settings to associate the IP address of our web host with our domain names. Wherever you bought your domain names, add an A record with the value of the IP address for your web host.
As always, DNS changes may take some time (up to 48 hours, but typically less) to propagate. What this means is that you might have to wait a bit before your domain name resolves to your website.
But otherwise, that’s it! The only thing left to do is to test everything out.
Open a web browser and load your websites. At this point, Nginx is now serving multiple websites from a single web host.
If you have any question or run into any hiccups, let me know in the comments below. Also to show your support, I’d be very appreciative if you subscribe to my YouTube channel which features loads of video tutorials on topics like this one.
One Response
Hey, Gianni DeCenzo,
Why did you quit your job as an actor on Kobra Kai?