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)
-
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.
-
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.
-
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.
Related Posts:
- Upgrade Nginx Ubuntu 18.04 Upgrade Nginx Ubuntu 18.04 Getting Started with Nginx Ubuntu 18.04 Installation Nginx is a high performance web server and reverse proxy. It is written in C and has ways to…
- Default Webserver Nginx Ubuntu 18.04 Default Webserver Nginx Ubuntu 18.04 What is Nginx? Nginx is an open source web server that is used to host websites or act as a reverse proxy for other web…
- Instal Nginx Ubuntu 18.04 Installing Nginx on Ubuntu 18.04 What is Nginx? Nginx is a lightweight, open source, high-performance web server designed for serving dynamic and static web content. It is capable of handling…
- Nginx Was Loaded Over Https But Requested An… Nginx Was Loaded Over Https But Requested an Insecure Stylesheet Understanding the Problem When the Nginx webserver is loaded over HTTPS, the server is expected to make secure connections with…
- 403 Forbidden Nginx 1.12 2 403 Forbidden Nginx 1.12 2 What is 403 Forbidden on Nginx 1.12 2? 403 Forbidden is an HTTP status code that indicates that the server is denying you access to…
- Install Nginx Phpmyadmin Centos 7 Install Nginx Phpmyadmin Centos 7 What is Nginx? Nginx is a web server that is used in Linux-based operating systems such as Centos 7. It is designed to handle high-traffic…
- Installing Nginx And Php In Docker Installing Nginx And Php In Docker Understanding Docker & Its Basics Docker is a virtualization platform for running applications in a container, without a virtual environment. It provides a bridge…
- How To Debian Nginx Php How To Debian Nginx Php Step 1 – Setup Your VPS The first step to setting up your web server is getting your VPS (Virtual Private Server). This is usually…
- Multi Host In Nginx Debian 9 Multi Host In Nginx Debian 9 Introduction Nginx is a web server that can be used for hosting multiple websites on a single server. This guide will help you set…
- Cara Install Nginx Ubuntu 14.04 Cara Install Nginx Ubuntu 14.04 Step 1: Update the Software Packages The first step in installing Nginx is to update the software packages in Ubuntu 14.04. This can be done…
- There Are No Sites-Available Folder Nginx . There Are No Sites-Available Folder Nginx What is Nginx Server? Nginx is an open-source web server that is used to deliver webpages to users. It is a free and…
- How To Set Rails On Nginx Ubuntu 18.04 How To Set Rails On Nginx Ubuntu 18.04 Step 1: Install Ruby Using RVM The first step for setting up Ruby on Rails on an Ubuntu 18.04 server with Nginx…
- Nginx Server_Name In Map Nginx Server_Name In Map What is an Nginx Server_Name? An Nginx Server_Name is the virtual host name associated with a web server. It is used to identify the server when…
- Sites-Enabled Nginx Conf Template Sites-Enabled Nginx Conf Template What is Nginx? Nginx is an open-source web server and reverse proxy used by the likes of Netflix, Ubisoft and WordPress. It is free and can…
- How To Configure Https Owncloud Using Nginx Ubuntu How To Configure Https Owncloud Using Nginx Ubuntu What Is OwnCloud? OwnCloud is an open-source file synchronization and hosting service. It is developed primarily to provide a web service, allowing…
- How To Install Flask Nginx On Ubuntu 1604 How To Install Flask Nginx On Ubuntu 1604 Introduction Flask is a web application framework based on Python. It is highly useful for web developers due to its flexibility and…
- Install Nginx Phpmyadmin Ubuntu 18.04 Install Nginx Phpmyadmin Ubuntu 18.04 Introduction to Nginx, PHP, and Ubuntu Nginx is an open-source, high-performance web server written in C and used to serve static and dynamic webpages. It…
- Install Letsencrypt Debian 9 Nginx Install Letsencrypt Debian 9 Nginx What is Letsencrypt? Letsencrypt is an open source, free, automated SSL service that provides users with the ability to secure their websites and use HTTPS…
- Include Etc Nginx Sites Enabled Conf Include Etc Nginx Sites Enabled Conf Introduction to Etc Nginx Sites Enabled The Etc Nginx sites enabled directory is located within the /etc/nginx directory. This directory contains all the nginx…
- Vps Webserver Nginx Php7 Mysql Vps Webserver Nginx Php7 Mysql What Is VPS Webserver? A VPS webserver is a Virtual Private Server (VPS) that has been configured to act as a web server. A VPS…
- How To Know If Nginx Is Working For A Directory How To Know If Nginx Is Working For A Directory Nginx is a popular open-source web server that is widely used in many websites and applications. It is known for…
- Replace Apache To Nginx Ubuntu 18 Replace Apache To Nginx Ubuntu 18 Installing Nginx Ubuntu 18 uses Nginx as its default web server, and it is pre-installed. However, if you want to use a fresh version…
- Nginx How To Override Nginx.Conf Conf.D Nginx How To Override Nginx.Conf Conf.D What is Nginx? Nginx is a high-performance web server that is commonly used for web hosting and reverse proxies. It has become increasingly popular…
- Nginx Multiple Apps Same Server Nginx Multiple Apps Same Server What is Nginx? Nginx is a popular open source web server software, responsible for taking user requests from a web browser, processing the request and…
- Create Domain Using Nginx Virtualmin Title: Create Domain Using Nginx Virtualmin Create Domain Using Nginx Virtualmin What is Nginx Virtualmin? Nginx Virtualmin is an automated website management platform from Virtualmin. It provides a powerful web…
- Delted Default Nginx Config File Deleted Default Nginx Config File What is an Nginx Config File? An Nginx (short for Engine X) config file is a text-based configuration file used to manage the behavior of…
- Change Html Folder To Www Nginx Change Html Folder To Www Nginx Introduction As websites continue to grow in size and complexity, it has become increasingly important to be able to customize and tweak the hosting…
- Forward Nginx To Another Subdomain Forward Nginx To Another Subdomain What is Nginx? Nginx is an open-source, high-performance web server originally developed by Igor Sysoev. Since its initial release in 2004, Nginx has become one…
- Add Domain Name To Nginx Please Don't Use Same Content. Add Domain Name To Nginx Introduction Nginx is one of the most popular web servers out there, and it is used by millions of websites.…
- Nginx Only Showing Default Page Nginx Only Showing Default Page What is Nginx? Nginx is an open-source web server that is used by many websites. Nginx is used to handle large traffic volumes and serve…