Detailed steps for configuring virtual hosts in nginx

Detailed steps for configuring virtual hosts in nginx

Virtual hosts use special software and hardware technologies, which divide a server host running on the Internet into a number of "virtual" hosts. Each virtual host can be an independent website with an independent domain name and 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 a standalone host.

insert image description here

With virtual hosts, there is no need to provide a separate Nginx server or run a separate set of Nginx processes for each website to be run. Virtual hosts provide the ability to run multiple websites on the same server and the same set of Nginx processes.

There are three ways to configure virtual hosts:

  • Domain name -based virtual hosting: different domain names, same IP (this method is the most widely used)
  • Port- based virtual hosting: Instead of using domain names or IP addresses to distinguish the contents of different sites, different TCP port numbers are used.
  • Virtual hosts based on IP addresses : different domain names, different IP addresses (need to add a network interface, not widely used) Based on IP addresses

insert image description here

Method 1: Multiple network cards and multiple IPs

Two physical network cards, two IP

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

[root@nginx 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 [root@nginx ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default	 

[root@nginx ~]# vim /etc/nginx/conf.d/ip.conf
# The virtual host server corresponding to the ens32 network card {
  listen 192.168.126.41:80;

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

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

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

Create a virtual host web page file directory and files

[root@nginx ~]# mkdir /ip_ens32
[root@nginx ~]# mkdir /ip_ens34

[root@nginx ~]# echo "ens32" > /ip_ens32/index.html
[root@nginx ~]# echo "ens34" > /ip_ens34/index.html

Check the syntax of the configuration file

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

Reload nginx service

[root@nginx ~]# systemctl reload nginx

test

[root@nginx ~]# curl 192.168.126.41
ens32
[root@nginx ~]# curl 192.168.126.42
ens34 

insert image description hereinsert image description here

Method 2: Single network card with multiple IP addresses

Configure multiple IPs for one 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-NIC multi-IP configuration

Port-based

insert image description here

Mostly used within the company when a domain name cannot be used or does not exist

Configuration

[root@nginx ~]# 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;
  }
}

[root@nginx ~]# mkdir /port_{81..82}
[root@nginx ~]# echo "81" > /port_81/index.html
[root@nginx ~]# echo "82" > /port_82/index.html

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

test

[root@nginx ~]# curl 192.168.126.41:81
81
[root@nginx ~]# curl 192.168.126.41:82
82 

insert image description hereinsert image description here

Based on domain name

insert image description here

Configuration

Generally, one domain name corresponds to one configuration file, which is easy to manage.

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

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

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

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

[root@nginx ~]# mkdir /test{1..2}
[root@nginx ~]# echo "test1" > /test1/index.html
[root@nginx ~]# echo "test2" > /test2/index.html

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

[root@nginx ~]# systemctl reload nginx

test

# Configure domain name resolution [root@nginx ~]# echo -e "192.168.126.41 test1.dxk.com\n192.168.126.41 test2.dxk.com" >> /etc/hosts
[root@nginx ~]# 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

[root@nginx ~]# curl test1.dxk.com
test1
[root@nginx ~]# curl test2.dxk.com
test2 

insert image description here
insert image description here
insert image description here

Here is the problem:

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

[root@nginx ~]# vim /etc/hosts
192.168.126.41 test1.dxk.com
192.168.126.41 test3.dxk.com # This should be test2.dxk.com, but it was written incorrectly, and the virtual host corresponding to the test3.dxk.com domain name does not exist

Visit the wrong domain name

[root@nginx ~]# curl test3.dxk.com
test1

# As you can see, web page information will still be returned

Because when configuring domain name resolution, although the domain name is written incorrectly, the IP is correct. In this case, the server will return the web page information of the first virtual host that meets the IP and port 80 to the client by default.

[root@nginx ~]# 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 is something to note

This is the end of this article about nginx virtual host. For more relevant nginx virtual host content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. 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
  • In-depth analysis of Nginx virtual host
  • Summary of some unpopular knowledge about virtual hosts in Nginx

<<:  Javascript tree menu (11 items)

>>:  HTML left and right layout example code

Recommend

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

Mysql delete duplicate data to keep the smallest id solution

Search online to delete duplicate data and keep t...

Linux kernel device driver proc file system notes

/***************** * proc file system************...

Vue implements the method of displaying percentage of echart pie chart legend

This article mainly introduces the pie chart data...

Understand the initial use of redux in react in one article

Redux is a data state management plug-in. When us...

The past two years with user experience

<br />It has been no more than two years sin...

How to choose between MySQL CHAR and VARCHAR

Table of contents VARCHAR and CHAR Types Conclusi...

Practical record of MySQL 5.6 master-slave error reporting

1. Problem symptoms Version: MySQL 5.6, using the...

CSS to achieve text on the background image

Effect: <div class="imgs"> <!-...

jQuery implements navigation bar effect with expansion animation

I designed and customized a navigation bar with a...

Vue implements small notepad function

This article example shares the specific code of ...

mysql 8.0.19 win10 quick installation tutorial

This tutorial shares the installation tutorial of...