Nginx 1.14 Create Virtual Host


Nginx 1.14 Create Virtual Host

Overview

Virtual hosting is a process for hosting multiple websites on a single physical server and IP address. Nginx version 1.14 is the most recent version that has the ability to host virtual hosts. Each website is hosted in a separate directory, and requests are routed to each virtual host via a specific Nginx configuration file in the sites-enabled folder. In this tutorial, we will show you how to configure Nginx version 1.14 to create multiple virtual hosts.

Prerequisites

To follow this tutorial, you will need:

  • A web server with Nginx 1.14 installed.
  • A domain name pointing to your web server’s IP address
  • A valid SSL certificate if you want to serve any HTTPS requests.

Step 1: Create Virtual Host Directories

The first step is to create the directory structure for each virtual host. Create a folder for each virtual host in your web server’s root directory, usually /var/www.

For example, if you have two domains, example.com and example2.com, create two directories in /var/www with the following commands:


mkdir /var/www/example.com
mkdir /var/www/example2.com

Next, you need to give the Apache user ownership of the folders. For Ubuntu-based systems, the Apache user is www-data. Enter the following commands to give the user ownership of the folders:


chown -R www-data:www-data /var/www/example.com
chown -R www-data:www-data /var/www/example2.com

Step 2: Create Nginx Configuration Files

Now you can create the configuration files for each virtual host. These files define how client requests are routed to each website in your virtual host setup.

Create a new configuration file for each virtual host. For example, if you have two domains, example.com and example2.com, create two configuration files named example.com and example2.com in /etc/nginx/sites-available:


touch /etc/nginx/sites-available/example.com
touch /etc/nginx/sites-available/example2.com

Next, open the configuration files in the text editor of your choice and configure them as follows. Replace example-server-name with the actual domain name:


server {
listen 80;
listen [::]:80;
server_name example-server-name;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}

Once you are done, save and close the files. To enable the virtual hosts, you need to create symbolic links from the configuration files in the sites-available directory to the sites-enabled directory.


ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/example2.com /etc/nginx/sites-enabled/

Now that you have created the configuration files and enabled the virtual hosts, you need to check the syntax of the configuration files.


nginx -t

If the syntax is correct, Nginx will display the following message:


Syntax OK

If you get any errors, make the necessary changes to your configuration files and run the command again.

Step 3: Restart Nginx

Once your configuration files are valid, restart Nginx for the changes to take effect:


service nginx restart

Now that Nginx has been restarted, you can test your virtual hosts. Open a web browser and visit each domain name. You should see a “Welcome to Nginx” page. If you see an error, make sure that your domain names are pointing to the correct IP address.

Conclusion

In this tutorial, you learned how to create virtual hosts in Nginx version 1.14. Virtual hosting allows you to host multiple websites on a single server and IP address, and each website can be configured with its own Nginx configuration file. Remember to check your configuration files for syntax errors before restarting Nginx.

Frequently Asked Questions (FAQs)

  1. How do I configure Nginx for virtual hosting?

    To configure Nginx for virtual hosting, you need to create the directory structure for each virtual host, create a configuration file for each virtual host, and create symbolic links to enable each virtual host. Once you have done that, you can restart Nginx for the changes to take effect.

  2. How do I check the syntax of my Nginx configuration files?

    To check the syntax of your Nginx configuration files, enter the command “nginx -t”. If the syntax is correct, Nginx will display the message “Syntax OK”. If you get any errors, make the necessary changes and run the command again.

  3. Do I need a separate IP address for each virtual host?

    No, you do not need a separate IP address for each virtual host. You can host multiple websites on a single IP address using Nginx’s virtual hosting feature.

Thank you for reading this article. Please read our other articles for more information.

Leave a Reply

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