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:
- 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…
- Setup Virtual Host Dengan Nginx Setup Virtual Host Dengan Nginx Overview Virtual host adalah salah satu cara untuk men-host lebih dari satu website dari satu server. Virtual host memungkinkan anda untuk menghubungkan beberapa domain dengan…
- 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…
- Setting Domain Ke Vps Nginx Setting Domain Ke Vps Nginx What is Nginx? Nginx is a powerful web server and popular application development platform that can be used to build efficient web applications. It was…
- 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…
- Vhost Nginx Redirection External Url Vhost Nginx Redirection External Url What is a Virtual Host setup for Nginx Redirection? A virtual host setup is a configuration on a webserver that allows it to host multiple…
- 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…
- Docker Nginx Location For Multiple Sites Docker Nginx Location For Multiple Sites If you are a web developer or an IT professional, you know that one of the major headaches in web development is finding a…
- Digital Ocean Ubuntu Server Nginx Docker Digital Ocean Ubuntu Server Nginx Docker Understanding Digital Ocean Digital Ocean is a cloud service provider that focuses on simplifying web infrastructure for cloud developers. They offer a platform where…
- Nginx Same Domain Different Port Nginx Same Domain Different Port Understanding Same Domain Different Port Concept Nginx is a powerful web server that supports different type of configurations. One of its features is its support…
- 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…
- 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…
- Nginx Responding To Any Domain Name Nginx Responding To Any Domain Name Understanding Nginx Nginx (pronounced “Engine X”) is a free, open-source, high-performance web server designed for better performance and scalability. It is used to serve…
- 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…
- How To Enable Subdomain In Nginx Digital Ocean How To Enable Subdomain In Nginx Digital Ocean What is a Subdomain? A subdomain is a subsection of a domain. It is used to create a separate website or page…
- 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.…
- Digitalocean Letsencrypt Nginx Ubuntu 18.04 Digitalocean Letsencrypt Nginx Ubuntu 18.04 Introduction Ubuntu 18.04 is the latest version of the popular Linux operating system. Digitalocean is a cloud hosting provider that specializes in hosting and managing…
- 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…
- Centos 7 Enable Nginx On Boot Centos 7 Enable Nginx On Boot Overview The ability to enable Nginx on Boot in CentOS 7 is a useful feature for web developers and server administrators. This article will…
- 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…
- 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…
- Create Virtual Host Nginx Centos 7 Create Virtual Host Nginx Centos 7 What is a Virtual Host? A virtual host is a server hosting multiple domain names on the same web server. This type of hosting…
- Nginx Ubuntu Access Virtual Host From Another… Nginx Ubuntu Access Virtual Host From Another Machine Over LAN Are you trying to access a virtual host set up using Nginx on Ubuntu from another machine over a LAN?…
- 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…
- Setup Vhost Nginx Debian 9.3 Setup Vhost Nginx Debian 9.3 Introduction This article will provide step-by-step instructions to setup Nginx Virtual Hosts (vhosts) on a Debian 9.3 system. Nginx is a web server software used…
- Where Is Nginx Document Root Where Is Nginx Document Root What Is Nginx? Nginx is an open source, high-performance web server and reverse proxy software popular on Linux and Unix. It is used to serve…
- Tutorial Start Nginx Ubuntu Webserver Tutorial Start Nginx Ubuntu Webserver Step 1: Prerequisites Before we start configuring Nginx as a web server on Ubuntu, we need to make sure we have the following prerequisites: A…
- Setting Sites Available Di Nginx Ubuntu Setting Sites Available Di Nginx Ubuntu Understanding Nginx and its Parts Nginx is a great tool for managing web applications. It allows you to host multiple websites, as well as…
- 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…
- 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…