Implementation of nginx virtual host settings based on domain name, port, and different IP

Implementation of nginx virtual host settings based on domain name, port, and different IP

1. Setting up nginx virtual host

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 function of running multiple websites on the same server and the same set of Nginx processes. Like Apache, Nginx can also configure multiple types of virtual hosts, namely IP-based virtual hosts, domain name-based virtual hosts, and port-based virtual hosts.
When using Nginx to build a virtual host server, each virtual Web site has an independent "serverf" configuration segment, and the IP address and port number that it listens on can be specified separately. Of course, the website name is also different.

1.1 Domain-based virtual hosting

1.11 Change the WIN10 mapping file host of the test system

1) Modify the host file

Modify the C:\Windows\System32\drivers\etc\hosts file of the Windows client and add the two domain names www.51xit.top and www.52xit.top. They both point to the same server IP address to enable different domain names to access different virtual hosts.

20.0.0.24 www.lph.com www.dzg.com

2) Start the nginx service to perform an initial test on the domain name

Whether testing www.lph.com or www.dzg.com, they both point to the website test homepage of server 20.0.0.24.

Visit in your browser: www.lph.com

insert image description here

Visit in the browser: www.dzg.com

insert image description here

What we need to achieve later is to access different outlets by accessing different domain names.

1.12 Catalog and test homepage of each website

[root@localhost~]# mkdir -p /var/www/html/lph/ ####Create the root directory of www.lph.com[root@localhost~]# mkdir -p /var/www/html/dzg/ ####Create the root directory of www.dzg.com[root@localhost~]# echo "www.lph.com" >> /var/www/html/lph/index.html
[root@localhost~]# echo "www.dzg.com" >> /var/www/html/dzg/index.html

1.13 Main configuration file

Modify the configuration file /usr/local/nginx/conf/nginx.conf, remove all server{} code segments in the configuration file, and add two new server{} segments corresponding to the two domain names.

1) Modification of configuration files

####Omitted####
  server {
    listen 80;
    server_name www.lph.com;
    charset utf-8;
    access_log logs/www.lph.com.access.log;
    location / {
      root /var/www/html/lph;
      index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }
  }
    server {
    listen 80;
    server_name www.dzg.com;
    charset utf-8;
    access_log logs/www.dzg.com.access.log;
    location / {
      root /var/www/html/dzg;
      index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }
  }
  ####Omitted####

2) Client test access www.lph.com

insert image description here

Visit www.dzg.com

insert image description here

1.2 Port-based virtual hosts

Only one IP address with different ports is needed to access different network points

1.21 Configuration file modification

server {
  listen 20.0.0.24:80;
  server_name www.lph.com;
  charset utf-8;
  access_log logs/www.lph.com.access.log;
  location / {
    root /var/www/html/lph;
    index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }
}
server {
  listen 20.0.0.24:8080;
  server_name www.dzg.com;
  charset utf-8;
  access_log logs/www.dzg8080.com.access.log;
  location / {
    root /var/www/html/dzg;
    index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }
}

1.22 Client Testing

Access www.lph.com:80 and access 20.0.0.24:80

insert image description here

Visit www.dzg.com:8080 and visit 20.0.0.24:8080

insert image description here

1.3 Virtual hosts based on different IP addresses

The host is configured with two IP addresses
20.0.0.24 192.168.100.24

1.31 Add a network card and set IP

[root@localhost ~]# nmcli connection #Copy the address of the newly added network card [root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vi ifcfg-ens36
NAME=ens36
UUID=ee2dccf4-cc4a-34bc-9cea-37e7d528cd27 #Paste the address of the newly added network card DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.100.26
NETMASK=255.255.255.0
GATEWAY=192.168.100.1

[root@localhost ~]# systemctl restart network
[root@localhost ~]# ifdown ens36
[root@localhost ~]# ifup ens36

########Open the computer cmd and ping it. If it succeeds, continue.

1.32 Modify the client's host file

20.0.0.0.24 www.lph.com
192.168.100.24 www.dzg.com

1.33 Modify the configuration file

server {
  listen 20.0.0.24:80;
  server_name www.lph.com;
  charset utf-8;
  access_log logs/www.lph.com.access.log;
  location / {
    root /var/www/html/lph;
    index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }
}
server {
  listen 192.168.100.24:80;
  server_name www.dzg.com;
  charset utf-8;
  access_log logs/www.dzg.com.access.log;
  location / {
    root /var/www/html/dzg;
    index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }
}

1.34 Client Testing

Visit www.lph.com and access 20.0.0.24

insert image description here

Visit www.dzg.com and visit 192.168.100.24

insert image description here

This is the end of this article about the implementation of nginx virtual host settings based on domain names, ports, and different IP addresses. For more relevant nginx virtual host settings, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx virtual host setting example (multiple website configuration)

<<:  Some problems that may be caused by inconsistent MySQL encoding

>>:  Implementation of element input box automatically getting focus

Recommend

Working principle and example analysis of Linux NFS mechanism

What is NFS? network file system A method or mech...

Write your HTML like this to make your code more compatible

For example, users who need screen reading softwar...

How to implement vertical text alignment with CSS (Summary)

The default arrangement of text in HTML is horizo...

Getting Started with Front-End Vue Unit Testing

Table of contents 1. Why do we need unit testing?...

The current better way to make select list all options when selected/focused

During development, I encountered such a requireme...

Implementation of Vue large file upload and breakpoint resumable upload

Table of contents 2 solutions for file upload Bas...

Introduction to Javascript DOM, nodes and element acquisition

Table of contents DOM node Element node: Text nod...

js and jquery to achieve tab status bar switching effect

Today we will make a simple case, using js and jq...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...

Quickly solve the problem of slow and stuck opening of input[type=file]

Why is it that when the input tag type is file an...

Let's talk in detail about how the NodeJS process exits

Table of contents Preface Active withdrawal Excep...

MySql 8.0.11 installation and configuration tutorial

Official website address: https://dev.mysql.com/d...