Three configuration methods of Ngxin virtual host

Foreword:

Nginx virtual host:
  virtual host is a special software and hardware technology, it can divide each computer on the network into multiple virtual hosts, each virtual host can independently provide web services to the outside, so that one host can be realized Provide multiple web services externally. Each virtual host is an independent website and independent domain name. From the perspective of website visitors, each virtual host is exactly the same as an independent host and does not affect each other.
Insert picture description here
Nginx can implement virtual host configuration, nginx supports three types of virtual host configuration.

  1. Divide virtual hosts by port (application: company internal website, web site management background)
  2. Divided by domain name (application: external website)
  3. Divided by IP (a host is bound to multiple IPs, which is less used)

Ready to work

System: Centos7
Nginx version: 1.18.0
Address: 10.8.161.9
Test machine system: win10
Address: 10.8.161.45 (same LAN)

1. Port division virtual host

  1. Configure ports 81, 82, and 83 as virtual host ports. Take the configuration of port 81 as an example. 82 and 83 are similar.
vim /etc/nginx/conf.d/server1.conf # 在 子配置文件目录conf.d创建81端口的主机

####一下是配置信息
server {
    
    
	    listen       81;           # 82、83更换端口即可
		server_name  localhost;    #本机模式下
        location / {
    
              
                    root   /var/www/nginx/server1;    #指定网页的目录
                    index index.html index.htm;       #指定访问的主页                                                                                      
                    }
         }
  1. Create 81 virtual host web pages and web files according to the directory in the configuration file
mkdir -p  /var/www/nginx/server1  创建网页目录
vim /var/www/nginx/server1/index.html            # 创建网页文件
显示内容:Server1 Host                             #创建内容
  1. Check whether the configuration is correct and restart the nginx service
[root@localhost ~]# nginx -t     # 检测命令,显示以下两行输出信息则配置正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok    
nginx: configuration file /etc/nginx/nginx.conf test is successful

systemctl  restart nginx         #重启nginx服务
  1. Open the browser, access the server address, followed by the port for configuring the virtual host.
    Insert picture description here

2. Domain name division virtual host

  1. Configure local DNS resolution on the test machine to facilitate testing. (This test machine win10)

    Insert picture description here
    Insert picture description here
    ③The
    test domain name can be pinged, and it is 10.8.161.9
    Insert picture description here
  2. Configure virtual host 1, similar to 2, 3
vim /etc/nginx/conf.d/server1.conf # 在 子配置文件目录conf.d创建www.sercer1.com主机

####一下是配置信息
server {
    
    
	    listen       80;          
		server_name www.server1.com;    #指定域名,2、3虚拟主机进行更换即可
        location / {
    
              
                    root   /var/www/nginx/server1;    #指定网页的目录
                    index index.html index.htm;       #指定访问的主页                                                                                      
                    }
         }
  1. Check whether the configuration is correct and restart the nginx service
[root@localhost ~]# nginx -t     # 检测命令,显示以下两行输出信息则配置正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok    
nginx: configuration file /etc/nginx/nginx.conf test is successful

systemctl  restart nginx         #重启nginx服务
  1. Open browser test
    Insert picture description here

Three. IP divides the virtual host

  1. Simulate multiple IP addresses with a network card on the server
    ①Check the current IP
ip -a  #查看当前网卡的ip地址

Insert picture description here②Create multiple virtual ip

ifconfig ens33:1 10.8.161.66/24  

Insert picture description here

  1. Configure virtual host 1, similar to 2, 3
vim /etc/nginx/conf.d/server1.conf # 在 子配置文件目录conf.d创建www.sercer1.com主机

####一下是配置信息
server {
    
    
	    listen       80;          
		server_name 10.8.161.66;    #指定虚拟的IP,2、3虚拟主机进行更换即可
        location / {
    
              
                    root   /var/www/nginx/server1;    #指定网页的目录
                    index index.html index.htm;       #指定访问的主页                                                                                      
                    }
         }
  1. Check whether the configuration is correct and restart the nginx service
[root@localhost ~]# nginx -t     # 检测命令,显示以下两行输出信息则配置正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok    
nginx: configuration file /etc/nginx/nginx.conf test is successful

systemctl  restart nginx         #重启nginx服务
  1. Open browser test
    Insert picture description here

================================================= ================================================= ================================================= ================================================= ================================================= =============================================
Hard browsing and watching, if right You are helpful, please like it (σ゚∀゚)σ…:*☆

Guess you like

Origin blog.csdn.net/qq_26129413/article/details/112584157