Software & AppsOperating SystemLinux

Hosting Multiple Websites on Nginx with One IP

Ubuntu 4

Hosting multiple websites on a single server is a common practice in web development. This is often done to save costs and manage resources more efficiently. One of the most popular web servers used for this purpose is Nginx. In this article, we’ll guide you through the process of hosting multiple websites on Nginx using a single IP address.

Quick Answer

Yes, it is possible to host multiple websites on Nginx using just one IP address. This can be achieved by either using subfolders or subdomains. By creating separate Nginx server blocks for each website and configuring them accordingly, you can efficiently manage and serve multiple websites on a single server.

What is Nginx?

Nginx is a powerful, open-source web server that’s known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. It’s commonly used for serving static websites, as a reverse proxy, and for load balancing.

Prerequisites

Before we start, make sure you have:

  • A server running Ubuntu or any other Linux distribution
  • Nginx installed on your server
  • Root or sudo access to your server
  • At least one domain name (for the subdomain method)

Method 1: Using Subfolders

This method involves hosting different websites in different subfolders of your web server’s root directory.

Step 1: Create Directories for Each Website

First, create a directory for each website. For example:

sudo mkdir -p /var/www/website1
sudo mkdir -p /var/www/website2

Step 2: Configure Nginx Server Blocks

Next, create a separate Nginx server block for each website. A server block is a section in the Nginx configuration that defines how to handle different types of traffic.

sudo nano /etc/nginx/sites-available/website1
sudo nano /etc/nginx/sites-available/website2

In each file, add a server block like this:

server {
 listen 80;
 server_name your_server_ip/website1;
 location / {
 root /var/www/website1;
 index index.html;
 }
}

Replace your_server_ip with your server’s IP address and website1 with the name of your website. Repeat this for each website, changing the server_name and root directives accordingly.

Step 3: Enable the Websites

Enable each website by creating a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/website1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website2 /etc/nginx/sites-enabled/

Finally, restart Nginx to apply the changes:

sudo systemctl restart nginx

Method 2: Using Subdomains

This method involves creating subdomains for each website. Each subdomain will point to the same IP address but will serve different content.

Step 1: Create Subdomains

First, create a subdomain for each website. This process varies depending on your domain registrar, so check their documentation for instructions.

Step 2: Configure Nginx Server Blocks

Next, create a separate Nginx server block for each subdomain:

sudo nano /etc/nginx/sites-available/website1
sudo nano /etc/nginx/sites-available/website2

In each file, add a server block like this:

server {
 listen 80;
 server_name website1.your_domain.com;
 location / {
 root /var/www/website1;
 index index.html;
 }
}

Replace website1.your_domain.com with your subdomain and website1 with the name of your website. Repeat this for each website, changing the server_name and root directives accordingly.

Step 3: Enable the Websites

Enable each website by creating a symbolic link to the sites-enabled directory, and then restart Nginx:

sudo ln -s /etc/nginx/sites-available/website1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website2 /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Conclusion

Hosting multiple websites on Nginx with one IP is an efficient way to manage your server resources. Whether you choose to use subfolders or subdomains depends on your specific needs and the nature of your websites. Remember to always test your configuration files for syntax errors before restarting Nginx to avoid downtime. Happy hosting!

What is the default location for Nginx configuration files?

The default location for Nginx configuration files is /etc/nginx/.

How can I check the syntax of my Nginx configuration files?

You can check the syntax of your Nginx configuration files by running the command sudo nginx -t. This command will validate the configuration files and display any syntax errors if present.

How do I reload Nginx after making changes to the configuration files?

To reload Nginx after making changes to the configuration files, use the command sudo systemctl reload nginx. This will apply the changes without restarting the entire Nginx process, minimizing downtime.

Can I use SSL/TLS certificates for my websites hosted on Nginx?

Yes, you can use SSL/TLS certificates to secure your websites hosted on Nginx. You can obtain SSL/TLS certificates from trusted certificate authorities or use free options like Let’s Encrypt. Nginx supports SSL/TLS configuration, and you can specify the certificate paths in your server blocks.

How can I set up basic authentication for a website hosted on Nginx?

To set up basic authentication for a website hosted on Nginx, you can use the auth_basic and auth_basic_user_file directives in your server block configuration. The auth_basic directive sets the authentication realm, and the auth_basic_user_file directive specifies the file containing the username and password combinations. You can generate the password file using the htpasswd command.

Leave a Comment

Your email address will not be published. Required fields are marked *