Configuring NGINX Virtual Hosts

A Comprehensive Guide

Introduction:
NGINX is a powerful web server and reverse proxy that allows you to host multiple websites on a single server using virtual hosts. In this guide, we’ll walk you through the process of configuring NGINX virtual hosts on a Linux server.

1. Install NGINX:

  • Before you can configure virtual hosts, you need to install NGINX on your server. You can do this using your package manager. For example, on Ubuntu, you can run:
  sudo apt update
  sudo apt install nginx

2. Create Directory Structure:

  • NGINX virtual hosts typically use separate directories for each website. Create a directory structure to store your website files. For example:
  /var/www/wooglet.com
  /var/www/secondsite.com

3. Configure Virtual Hosts:

  • Navigate to the NGINX configuration directory, usually located at /etc/nginx/sites-available.
  • Create a new configuration file for each virtual host. For example:
  sudo nano /etc/nginx/sites-available/wooglet.com
  • Inside each configuration file, define the virtual host settings, including the server name, root directory, access logs, and error logs. Here’s an example configuration for example.com:
  server {
      listen 80;
      server_name wooglet.com www.wooglet.com;
      root /var/www/wooglet.com;
      index index.html;

      access_log /var/log/nginx/wooglet.com_access.log;
      error_log /var/log/nginx/wooglet.com_error.log;

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

4. Enable Virtual Hosts:

  • After creating the configuration files, you need to enable them by creating symbolic links to the sites-enabled directory. Use the following command:
  sudo ln -s /etc/nginx/sites-available/wooglet.com /etc/nginx/sites-enabled/

5. Test Configuration and Reload NGINX:

  • Check the NGINX configuration for syntax errors:
  sudo nginx -t
  • If there are no errors, reload NGINX to apply the changes:
  sudo systemctl reload nginx

6. Repeat for Additional Virtual Hosts:

  • Repeat the above steps for each additional virtual host you want to configure on your server.

7. Test Virtual Hosts:

  • Open your web browser and visit each of your configured domain names to verify that the virtual hosts are working correctly.


By following these steps, you’ve successfully configured NGINX virtual hosts on your server. You can now host multiple websites on a single server using NGINX, providing a scalable and efficient web hosting solution.

Leave a Reply

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