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:
- Setup Virtual Hosts In Nginx Setup Virtual Hosts In Nginx Understanding The Virtual Hosts Concept The Virtual Hosts concept is an important part of installing Nginx. It allows you to host multiple websites on a…
- 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…
- How To Add Domain In Nginx How To Add Domain In Nginx Introduction To Nginx Nginx (Internet Information Server) is a popular open-source web server that is able to serve a variety of content, from simple…
- Centos 7 Migrate From Apache To Nginx Centos 7 Migrate From Apache To Nginx Introduction CentOS 7 is a popular Linux operating system (OS) that is used in many businesses and organizations. It is a stable and…
- Cannot Get Index.Html Nginx Cannot Get Index.Html Nginx What is Nginx? Nginx is a web server software designed for high-traffic websites and web applications. It is an open source reverse proxy server for HTTP,…
- 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…
- Seting Var Ww On Nginx Centos7 Setting Var WW On Nginx Centos7 Overview Var WW is a powerful tool used to configure and control Nginx websites. It provides technical and graphical interface to manage the Nginx…
- Cara Install Nginx Debian 4.9 Cara Install Nginx Debian 4.9 Introduction Debian 4.9 is a major release of the Debian Linux-based operating system. It is the first major version of the operating system to be…
- 403 Forbidden Nginx Ubuntu 14.04 403 Forbidden Nginx Ubuntu 14.04 What Is a 403 Forbidden Error? A 403 Forbidden error is an HTTP status code that denotes that a server, upon receiving a request from…
- Reinstall Nginx Ubuntu 18.04 Reinstall Nginx Ubuntu 18.04 What is Nginx? Nginx is an open source, high-performance web server application designed to serve web traffic with lightning-fast speed and robust stability. Nginx is one…
- Create Subdomain Nginx Debian 9 Server Create Subdomain Nginx Debian 9 Server Understanding Subdomain A subdomain is a subsection of a domain that a user can create to host one or more websites, usually for a…
- Install Nginx And Php On Ubuntu Install Nginx And Php On Ubuntu A Comprehensive Tutorial to Install Nginx And Php On Ubuntu Nginx and PHP are two of the most popular web server and scripting language…
- Web Server Nginx Install Di Centos 7 Web Server Nginx Install Di Centos 7 Introduction to Nginx Nginx is a popular open source web server used to host websites and other applications. It is known for its…
- How To Configure Nginx Debian How To Configure Nginx Debian Introduction to Nginx Debian Nginx is a web server and reverse proxy that is used to host websites and manage incoming traffic over the world…
- Nginx Configure Multiple Sites Differen Port Nginx Configure Multiple Sites Different Port Introduction to Nginx Nginx is a web server software developed by Igor Sysoev in 2002. It’s a high performance web server with a large…
- How To Configure Virtual Host In Nginx On Centos 7 How To Configure Virtual Host In Nginx On Centos 7 Introduction We all need to configure virtual host on Nginx webserver on CentOS 7 in order to place multiple websites…
- Install Nginx Ubuntu 16.04 For Odoo Install Nginx Ubuntu 16.04 For Odoo Install Ubuntu Server 16.04 Before we can install Nginx for Odoo we need to install Ubuntu 16.04 on our server. Ubunutu 16.04 is still…
- Nginx Centos 7.6 Virtual Host Nginx Centos 7.6 Virtual Host Introduction to Nginx Virtual Hosts Virtual Hosts, also called Virtual Servers, are a very important function of web hosting. They allow multiple websites to run…
- Nginx Static Location For Multiple Django Nginx Static Location For Multiple Django What is Nginx? Nginx is an open source web server written in C that is well-known for its performance and speed. Nginx is one…
- Laradock Nginx Exit Duplicate Default Server For… Laradock Nginx Exit Duplicate Default Server For 0.0.0.0:80 In What is Nginx Derived From? Nginx is derived from an open-source and high-performance HTTP server developed by Russian developer Igor Sysoev.…
- Konfigurasi Web Server Dengan Nginx Virtual Host Konfigurasi Web Server Dengan Nginx Virtual Host Apa itu Nginx? Nginx (atau NGINX) adalah salah satu dari yang paling terkenal dan populer web server yang digunakan di seluruh dunia. Ini…
- Nginx Responding To Any Doimain Name Nginx Responding To Any Domain Name Web servers such as Nginx, Apache, and IIS are all used to respond to requests from web browsers. These requests come from domain names,…
- Setting Serverblock For Domain Using Nginx On Ubuntu 18.04 Setting ServerBlock For Domain Using Nginx On Ubuntu 18.04 Introduction Nginx is a powerful open-source web server that can be used for serving static, dynamic websites and applications. Nginx is…
- 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…
- 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…
- 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…
- Laravel Nginx Default Multiple Site Laravel Nginx Default Multiple Site What is Nginx? Nginx is a popular open source web server used for hosting websites on the internet. It is designed for high-traffic websites and…
- Docker Nginx Php Mysql In Xampp Docker Nginx Php Mysql In Xampp Overview of Docker Nginx Php Mysql in Xampp Docker Nginx Php Mysql in Xampp is a powerful web development platform used for developing complex…
- Stup Nginx In A Domain Medium Steps to Setup Nginx In A Domain Medium Introduction To Nginx Nginx (pronounced engine-x) is a lightweight, open source web server that was originally designed as a proxy server for…
- Nginx Multiple Web Sites One Ip Nginx Multiple Web Sites One Ip Introduction Nginx is a powerful and popular web server used by millions of websites and web application around the world. It is highly performant,…