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

WeChat Mini Program User Authorization Best Practices Guide

Preface When developing WeChat applets, you often...

Example code of CSS layout at both ends (using parent's negative margin)

Recently, during the development process, I encou...

Django online deployment method of Apache

environment: 1. Windows Server 2016 Datacenter 64...

MySQL performance optimization tips

MySQL Performance Optimization MySQL is widely us...

Why Google and Facebook don't use Docker

The reason for writing this article is that I wan...

The use of setState in React and the use of synchronous and asynchronous

In react, if you modify the state directly using ...

Vue implements chat interface

This article example shares the specific code of ...

Using Vue3 (Part 1) Creating a Vue CLI Project

Table of contents 1. Official Documentation 2. Cr...

How to allow remote connection in MySql

How to allow remote connection in MySql To achiev...

Time zone issues with Django deployed in Docker container

Table of contents Time zone configuration in Djan...

Solution to the problem of MySQL deleting and inserting data very slowly

When a company developer executes an insert state...

Methods and problems encountered in installing mariadb in centos under mysql

Delete the previously installed mariadb 1. Use rp...