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:
- 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…
- 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…
- 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 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.…
- Nginx Listen Multiple Ip Addresses Nginx Listen Multiple Ip Addresses What is Nginx? Nginx is a open-source web server created by Igor Sysoev and first publicly released in 2004. Since its release, Nginx has become…
- Nginx Proxy Cache Redis Wordpress Centos Nginx Proxy Cache Redis Wordpress Centos Understanding Nginx Proxy Cache Nginx Proxy Cache is a web-acceleration software from Nginx, Inc. It is designed to speed up dynamic web content delivery.…
- 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…
- 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…
- 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…
- 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…
- 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…
- 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…
- 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…
- Cara Buat Virtual Host Di Nginx Cara Buat Virtual Host Di Nginx Apa Itu Virtual Host? Virtual Host adalah sebuah teknologi jaringan yang memungkinkan sebuah mesin untuk dapat melayani permintaan berbagai macam website, dengan tidak memerlukan…
- 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…
- 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…
- Nginx Css And Image Not Showing Centos Nginx Css And Image Not Showing Centos Common Problems When Nginx CSS And Images Aren't Showing When you're working with Nginx on the Centos operating system, you may have run…
- 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…
- 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…
- 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…
- Nginx Php-Fpm Php Mariadb Mysql Centos 7.2 Digitalocean Nginx, Php-Fpm, Php, Mariadb, Mysql, Centos 7.2 Digitalocean Introduction: What is Nginx? Nginx is a powerful web server that was first released in 2004. It is known for being a…
- 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?…
- 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…
- 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…
- Centos 6 Nginx Multiple Php Version Centos 6 Nginx Multiple PHP Versions What is CentOS 6? CentOS 6 is a Linux-based operating system. It is a free, open-source operating system that is based on Red Hat…
- 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…
- How To Start Nginx On Centos 7 How To Start Nginx On Centos 7 Overview Nginx is a high-performance web server that is used to serve web pages and applications. It is a widely-used open source software…
- Failed Install Nginx On Centos Failed Install Nginx On Centos What is Nginx? Nginx is an open source web server designed to handle high traffic websites. It has proven to be reliable over the years…
- 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…
- 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…