In-depth analysis of Nginx virtual host

In-depth analysis of Nginx virtual host

1. Virtual Host

1.1 Virtual Host Concept

For Nginx, each virtual host is equivalent to a site that is independent of each other in the same server, so that one host can provide multiple web services to the outside world. Each virtual host is independent and does not affect each other.

1.2 Virtual Host Type

Virtual host configuration can be achieved through Nginx. Nginx supports three types of virtual host configuration:

  • IP-based virtual hosting (less commonly used)
  • Domain-based virtual hosting
  • Port-based virtual hosts

2. Based on IP virtual host

2.1 Configuring multiple IP addresses

 [root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0
 [root@nginx ~]# ip addr | grep 172
 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0
 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0

Tip: Add multiple IP addresses on the same host as above.

2.2 Create a site directory

 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/
 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/
 [root@nginx ~]# echo '<h1>Ipvhost01</h1>' > /usr/share/nginx/ipvhost01/index.html
 [root@nginx ~]# echo '<h1>Ipvhost02</h1>' > /usr/share/nginx/ipvhost02/index.html

2.3 Configure virtual host

 [root@nginx ~]# vi /etc/nginx/conf.d/ipvhost.conf
 server {
 listen ; #Listen port server_name ipvhost.odocker.com ...; #Configure virtual host name and IP
 location / {
 root /usr/share/nginx/ipvhost; #Request matching path index index.html; #Specify homepage access_log /var/log/nginx/ipvhost.access.log main;
 error_log /var/log/nginx/ipvhost.error.log warn;
 }
 }
 server {
 listen ;
 server_name ipvhost.odocker.com ...;
 location / {
 root /usr/share/nginx/ipvhost;
 index index.html;
 access_log /var/log/nginx/ipvhost.access.log main;
 error_log /var/log/nginx/ipvhost.error.log warn;
 }
 }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #Check the configuration file [root@nginx ~]# nginx -s reload #Reload the configuration file

2.4 Confirmation and Verification

Browser access: ipvhost01.odocker.com.
clipboard
Browser access: ipvhost02.odocker.com.
clipboard

3. Domain name based virtual host

3.1 Create a site directory

 [root@nginx ~]# mkdir /usr/share/nginx/webvhost01/
 [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/
 [root@nginx ~]# echo '<h1>Webvhost01</h1>' > /usr/share/nginx/webvhost01/index.html
 [root@nginx ~]# echo '<h1>Webvhost02</h1>' > /usr/share/nginx/webvhost02/index.html

3.2 Configure virtual host

 [root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf
 server {
 listen ;
 server_name webvhost.odocker.com;
 location / {
 root /usr/share/nginx/webvhost;
 index index.html;
 access_log /var/log/nginx/webvhost.access.log main;
 error_log /var/log/nginx/webvhost.error.log warn;
 }
 }
 server {
 listen ;
 server_name webvhost.odocker.com;
 location / {
 root /usr/share/nginx/webvhost;
 index index.html;
 access_log /var/log/nginx/webvhost.access.log main;
 error_log /var/log/nginx/webvhost.error.log warn;
 }
 }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #Check the configuration file [root@nginx ~]# nginx -s reload #Reload the configuration file

3.3 Confirmation and Verification

Browser access: webvhost01.odocker.com.
clipboard
Browser access: webvhost02.odocker.com.
clipboard

Four port-based virtual hosts

4.1 Create a site directory

[root@nginx ~]# mkdir /usr/share/nginx/portvhost01/
 [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/
 [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost01/index.html
 [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost02/index.html

4.2 Configure virtual host

 [root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf
 server {
 listen ;
 server_name portvhost.odocker.com;
 location / {
 root /usr/share/nginx/portvhost;
 index index.html;
 access_log /var/log/nginx/portvhost.access.log main;
 error_log /var/log/nginx/portvhost.error.log warn;
 }
 }
 server {
 listen ;
 server_name portvhost.odocker.com;
 location / {
 root /usr/share/nginx/portvhost;
 index index.html;
 access_log /var/log/nginx/access_portvhost.log main;
 }
 }
 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #Check the configuration file [root@nginx ~]# nginx -s reload #Reload the configuration file

4.3 Confirmation and Verification

Browser access: portvhost01.odocker.com:8080
clipboard
Browser access: portvhost02.odocker.com:8081
clipboard

This is the end of this article about Nginx virtual host. For more relevant Nginx virtual host content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to set up virtual hosts and specified access paths in Nginx
  • Nginx virtual host setting example (multiple website configuration)
  • Three ways to configure Nginx virtual hosts (based on domain names)
  • Detailed explanation of nginx virtual host configuration example
  • Detailed steps for configuring virtual hosts in nginx
  • Summary of some unpopular knowledge about virtual hosts in Nginx

<<:  About MYSQL, you need to know the data types and operation tables

>>:  Vue+Openlayer uses modify to modify the complete code of the element

Recommend

CSS3 to achieve dynamic background gradient effect

Learning CSS3 is more about getting familiar with...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

JavaScript to implement click to switch verification code and verification

This article shares the specific code of JavaScri...

Play and save WeChat public account recording files (convert amr files to mp3)

Table of contents Audio transcoding tools princip...

Detailed steps for Linux account file control management

In the Linux system, in addition to various accou...

Ten important questions for learning the basics of Javascript

Table of contents 1. What is Javascript? 2. What ...

Docker uses a single image to map to multiple ports

need: The official website's resource server ...

How to implement responsive layout in vue-cli

When we are doing front-end development, we will ...

Detailed explanation of the minimum width value of inline-block in CSS

Preface Recently, I have been taking some time in...

Summary of Linux nc command

NC's full name is Netcat (Network Knife), and...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

Linux process management tool supervisor installation and configuration tutorial

Environment: CentOS 7 Official documentation: htt...

Detailed installation tutorial for MySQL zip archive version (5.7.19)

1. Download the zip archive version from the offi...

Vue codemirror realizes the effect of online code compiler

Preface If we want to achieve the effect of onlin...