Detailed steps for nginx configuring virtual hosts

Time:2021-9-26

The virtual host uses special software and hardware technology. It divides a server host running on the Internet into a “virtual” host. Each virtual host can be an independent website, have an independent domain name, and have complete Internet server functions (WWW, FTP, email, etc.), Virtual hosts on the same host are completely independent. From the perspective of website visitors, each virtual host is exactly the same as an independent host.

在这里插入图片描述

Using virtual host, there is no need to provide a separate nginx server or run a group of nginx processes for each website to run. Virtual host provides the function of running multiple websites on the same server and the same group of nginx processes.

There are three ways to configure a virtual host:

  • be based ondomain nameVirtual host: different domain names and the same IP (this method is most widely used)
  • be based onportVirtual host: it does not use domain name and IP to distinguish the contents of different sites, but uses different TCP port numbers
  • be based onIP addressVirtual host: different domain names and different IP addresses (network interfaces are required, which are not widely used) are based on IP addresses

在这里插入图片描述

Method 1: multiple network cards and multiple IP addresses

Two physical network cards, two IP

#Two physical network cards ens32 and ens34
[[email protected] network-scripts]# ifconfig ens32 | awk 'NR==2 {print $2}'  
192.168.126.41

[[email protected] network-scripts]# ifconfig ens34 | awk 'NR==2 {print $2}'  
192.168.126.42

Edit the configuration file to create a virtual host based on each IP

#To prevent the / etc / nginx / conf.d/default.conf configuration file from being affected, rename it
[[email protected] ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default	 

[[email protected] ~]# vim /etc/nginx/conf.d/ip.conf
#Virtual host corresponding to ens32 network card
server {
  listen 192.168.126.41:80;

  location / {
    root /ip_ens32;
    index index.html;
  }
}

#Virtual host corresponding to ens34 network card
server {
  listen 192.168.126.42:80;

  location / {
    root /ip_ens34;
    index index.html;
  }
}

Create web page file directory and files of virtual host


[[email protected] ~]# mkdir /ip_ens32
[[email protected] ~]# mkdir /ip_ens34

[[email protected] ~]# echo "ens32" > /ip_ens32/index.html
[[email protected] ~]# echo "ens34" > /ip_ens34/index.html

Check the syntax of the configuration file


[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Overloaded nginx service


[[email protected] ~]# systemctl reload nginx

test


[[email protected] ~]# curl 192.168.126.41
ens32
[[email protected] ~]# curl 192.168.126.42
ens34

在这里插入图片描述在这里插入图片描述

Mode 2: single network card multi IP

Configure multiple IP addresses for a physical network card

IP addr add IP / mask dev network card name

#Delete
IP addr del IP / mask dev network card name

The remaining steps are the same as the above multi network card and multi IP configuration

Port based

在这里插入图片描述

It is mostly used within the company. When the domain name cannot be used or there is no domain name

to configure


[[email protected] ~]# vim /etc/nginx/conf.d/port.conf
server {
  listen 81;

  location / {
    root /port_81;
    index index.html;
  }
}

server {
  listen 82;

  location / {
    root /port_82;
    index index.html;
  }
}

[[email protected] ~]# mkdir /port_{81..82}
[[email protected] ~]# echo "81" > /port_81/index.html
[[email protected] ~]# echo "82" > /port_82/index.html

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# systemctl reload nginx

test


[[email protected] ~]# curl 192.168.126.41:81
81
[[email protected] ~]# curl 192.168.126.41:82
82

在这里插入图片描述在这里插入图片描述

Domain name based

在这里插入图片描述

to configure

Generally, a domain name corresponds to a configuration file, which is convenient for management


[[email protected] ~]# vim /etc/nginx/conf.d/test1.dxk.com.conf
server {
  listen 80;
  server_name test1.dxk.com;

  location / {
    root /test1;
    index index.html;
  }
}

[[email protected] ~]# vim /etc/nginx/conf.d/test2.dxk.com.conf
server {
  listen 80;
  server_name test2.dxk.com;

  location / {
    root /test2;
    index index.html;
  }
}

[[email protected] ~]# mkdir /test{1..2}
[[email protected] ~]# echo "test1" > /test1/index.html
[[email protected] ~]# echo "test2" > /test2/index.html

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[[email protected] ~]# systemctl reload nginx

test

#Configure domain name resolution
[[email protected] ~]# echo -e "192.168.126.41 test1.dxk.com\n192.168.126.41 test2.dxk.com" >> /etc/hosts
[[email protected] ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.126.41 test1.dxk.com
192.168.126.41 test2.dxk.com

[[email protected] ~]# curl test1.dxk.com
test1
[[email protected] ~]# curl test2.dxk.com
test2

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Here’s a question:

If the domain name resolution is configured incorrectly, the web page content will be returned when accessing the wrong domain name (virtual host without the wrong domain name configured).

[[email protected] ~]# vim /etc/hosts
192.168.126.41 test1.dxk.com
192.168.126.41 test3.dxk.com # here should have been test2.dxk.com, but it was written incorrectly and the virtual host corresponding to the domain name of test3.dxk.com did not exist

Access the wrong domain name

[[email protected] ~]# curl test3.dxk.com
test1

#You can see that the web page information will still be returned

When configuring domain name resolution, although the domain name is written incorrectly, but the IP is correct, the server will return the web page information of the first virtual host satisfying the IP and port 80 to the client by default


[[email protected] ~]# ll /etc/nginx/conf.d/
-rw-r--r--. 1 root root  112 Jul  3 21:23 test1.dxk.com.conf
-rw-r--r--. 1 root root  112 Jul  3 21:22 test2.dxk.com.conf

This needs attention

This is the end of this article about nginx virtual host. For more information about nginx virtual host, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]