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 or applications on a single server. This is done by mapping domains, subdomains and IP addresses to different domains or application. When a user requests for a particular domain or IP address, it is served with the respective web files and applications. In this article, we’ll discuss about how you can set up multiple virtual hosts in Nginx web server on CentOS 7.
Steps To Configure Virtual Hosts On Nginx
Follow the steps mentioned below in order to configure virtual host in Nginx on CentOS 7.
- Login to your instance (remote or local) of Nginx web server and type “sudo yum update” in order to update the entire operating system.
- Type in “sudo yum install nginx” to install nginx and it’s components on your server.
- After the installation is completed, configure the nginx virtual host by creating a new configuration file and modifying the existing configuration file.
- Create a new configuration file with the following command “sudo nano /etc/nginx/sites-available/default”.
- In the new configuration file, add the following lines of code –
server {
listen 80;
server_name domainname.com www.domainname.com;
root /var/opt/html/domainname.com;
index index.html index.htm;
error_log /var/log/nginx/domainname.com/error.log;
access_log /var/log/nginx/domainname.com/access.log;
}
- After that, create the directory structure for your website as stated in the configuration. To do that, use the command “mkdir –p /var/opt/html/domainname.com”.
- Create a sample index.html file in the particular directory with the help the following command “nano /var/opt/html/domainname.com/index.html”.
- In the file, enter the following content –
test web page
Virtual Host worked!
- Save the file and exit.
- Once that is done, restart the nginx server with the command “sudo service nginx restart”.
- Verify that the virtual host has been configured correctly by accessing the website url in the browser. The page must contain the content specified above.
Setting up Multiple virtual hosts
The process for setting up multiple virtual hosts is essentially the same as setting up a single virtual host. The only difference here is that you need to create multiple configuration files with the different domain name and root directory for each virtual host.
Once the new configuration file is created, add it to the available sites directory as stated in the steps above. Post that, activate the virtual host with the following command “sudo ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/domainname.com”.
You should repeat the above steps for each virtual host you have. Post that, restart the nginx server with the command “sudo service nginx restart”. You can now access the new website in the browser.
Conclusion
Virtual hosting provides great flexibility to host multiple applications or websites on a single server. However, it is important to be careful while configuring the virtual hosts as even a small mistake can lead to the website being down.
We hope that the article has helped you in setting up virtual hosts on Nginx web server on CentOS 7. If you have any queries, feel free to ask in the comments section.
FAQs
- Q: What is virtual hosting?
A: Virtual hosting is the process of hosting multiple websites or applications on a single server. Each website is assigned with its own domain name, and has its own IP address.
- Q: How to enable virtual hosting in Nginx?
A: Virtual hosting can be enabled in Nginx by creating configuration files, and triggering the change with the command “sudo service nginx restart”.
- Q: How to setup multiple virtual hosts in Nginx?
A: Setting up multiple virtual hosts require you to create multiple configuration files. Once the new configuration files have been created, they need to be added to the available sites directory. Post that, you can activate the virtual host by using the command “sudo ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/domainname.com”.
Thank you for reading this article. Please read other articles for more information.
Related Posts:
- Install Nginx 1.17 Centos 8 Install Nginx 1.17 Centos 8 Introduction to Nginx Nginx is one of the most popular web servers in the world. It is reliable, free, and open source software. It is…
- 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…
- How To Setup Nginx On Ubuntu How To Setup Nginx On Ubuntu Introduction Nginx is a powerful web server that is very popular among Linux users. It is open-source and comes with great features such as…
- 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…
- Configure Nginx As Proxy Server Configure Nginx As Proxy Server Introduction Nginx is a software application used for serving dynamic web pages and web content. It is an open source, lightweight and highly modular web…
- 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…
- 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…
- Laravel Nginx Without Custom Domain Laravel Nginx Without Custom Domain Overview of Laravel Nginx Without Custom Domain Laravel is an open-source PHP web framework used to create powerful web applications. It is based on the…
- 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…
- Nginx Dev Mapper Centos-Root Is 100 Full Nginx Dev Mapper CentOs-Root Is 100 Full What Is Nginx Dev Mapper? Nginx Dev Mapper is a file system space mapping tool for the Linux operating system and is shipped…
- 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?…
- Reverse Proxy Nginx Centos 7 Reverse Proxy Nginx Centos 7 What is a Reverse Proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or…
- 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…
- 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…
- 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…
- Install Web Server Nginx Centos 7 Install Web Server Nginx Centos 7 Introduction Are you looking for a way to set up a web server on your Linux-based system? If so, then installing Nginx on CentOS…
- 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…
- Virtual Host Nginx Ubuntu 16.04 Virtual Host Nginx Ubuntu 16.04 Introduction A virtual host (also known as Virtual Private Server or VPS) is a service that allows a single physical server to host multiple websites.…
- 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,…
- 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…
- Install Nginx With Waf Centos 7 Install Nginx With Waf Centos 7 Overview Nginx is a popular open-source web server used for hosting websites and applications. It is widely used due to its high performance, efficient…
- 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…
- Nginx Windows Create Virtual Host Nginx Windows Create Virtual Host What Is Nginx? Nginx is an open source web server software. First released in 2004, it has steadily become the preferred choice of web administrators…
- 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…
- Setup Centos 7 Nginx With Php Setup Centos 7 Nginx With Php Overview of Centos7 Nginx and PHP Centos 7 is one of the most popular operating systems for web servers. Many businesses use Centos 7…
- 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…
- 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…
- 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…
- Nginx Proxy_Pass To Tomcat Nginx Proxy_Pass To Tomcat Introduction The Nginx Proxy_Pass directive is used to provide a secure and efficient way to redirect requests from a web server to a Tomcat Application Server.…
- Server_Name_In_Redirect Nginx Server_Name_In_Redirect Nginx What is Server Name In Redirect (SNIR)? Server Name In Redirect (SNIR) is a method to use an Nginx webserver to route requests from multiple domains to a…