Configuración virtual de nginx (host virtual)

¿Qué es el alojamiento web?
El host virtual es una tecnología especial de software y hardware. Puede dividir cada computadora en la red en múltiples hosts virtuales. Cada host virtual puede proporcionar servicios www de forma independiente al mundo exterior, de modo que un host pueda proporcionar múltiples web externamente. Los servicios son independientes de cada host virtual y no se afectan entre sí.

[Falló la transferencia de la imagen del enlace externo, el sitio de origen puede tener un mecanismo anti-sanguijuelas, se recomienda guardar la imagen y subirla directamente (img-yA01vk4n-1598623023772) (assets / 1561605672295.png)]

Nginx puede implementar la configuración de host virtual, nginx admite tres tipos de configuración de host virtual.
1. Host virtual basado en el nombre de dominio (server_name para distinguir el host virtual-aplicación: sitio web externo)
2. Host virtual basado en ip (un host está vinculado a varias direcciones IP)
3. Host virtual basado en el puerto (puerto para distinguir virtual Aplicación de host: el sitio web interno de la empresa, antecedentes de gestión del sitio web externo)

1. Alojamiento virtual basado en nombre de dominio

1. Configure máquinas virtuales distinguidas por nombres de dominio

[root@localhost ~]# cat /etc/nginx/nginx.conf
worker_processes  4;

#error_log  logs/error.log;
worker_rlimit_nofile 102400;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server {
    
    
        listen       80;
        server_name  web.testpm.com;
        location / {
    
    
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate	2k;
        	}
        }
    
    server {
    
    
        listen       80;
        server_name  web.1000phone.com;
        location / {
    
    
            root   /1000phone/html;
            index  index.html index.htm;
        	}
        }
}

2. Cree un archivo de índice para la máquina virtual cuyo nombre de dominio es web.1000phone.com

[root@localhost ~]# mkdir -p /1000phone/html
[root@localhost ~]# vim /1000phone/html/index.html
<html>
<p>
this is my 1000phone
</p>
</html>

3. Vuelva a cargar el archivo de configuración

# 如果编译安装的执行
[root@nginx]# /usr/local/nginx/sbin/nginx -s reload
# 如果 yum 安装的执行
[root@nginx]# nginx -s reload

4. Configure la asignación de rutas
en el lado del cliente . Agregue dos líneas al archivo C: \ Windows \ System32 \ drivers \ etc \ hosts (linux: / etc / hosts)

10.0.105.199 web.testpm.com
10.0.105.199 web.1000phone.com

5. Visita de prueba

Entrada del navegador: http://web.testpm.com/

Entrada del navegador: http://web.1000phone.com/

2. Host virtual basado en IP

[root@localhost ~]# ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:17:f1:af brd ff:ff:ff:ff:ff:ff
    inet 10.0.105.199/24 brd 10.0.105.255 scope global dynamic ens33
       valid_lft 81438sec preferred_lft 81438sec
    inet6 fe80::9d26:f3f0:db9c:c9be/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24  #要保证添加的IP没有被使用(给网卡再添加一个ip)
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.105.199  netmask 255.255.255.0  broadcast 10.0.105.255
        inet6 fe80::9d26:f3f0:db9c:c9be  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)
        RX packets 9844  bytes 1052722 (1.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5567  bytes 886269 (865.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.105.201  netmask 255.255.255.0  broadcast 10.0.105.255
        ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)

2、配置通过ip区分的虚拟机
[root@localhost ~]# cat /etc/nginx/nginx.conf
user  root;
worker_processes  4;

#error_log  logs/error.log;
worker_rlimit_nofile 102400;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    server {
    
    
        listen       10.0.105.199:80;
        server_name  web.testpm.com;
        location / {
    
    
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate	2k;
        }
        
     server {
    
    
        listen       10.0.105.201:80;
        server_name  web.testpm.com;
        location / {
    
    
            root   /1000phone/html/;
            index  index.html index.htm;
        	}
        }
}
3、重新加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
4、 测试访问
浏览器输入:http://10.0.105.199
浏览器输入:http://10.0.105.201
5、补充
-- 删除绑定的vip
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 down
重启一下nginx
[root@localhost ~]# systemctl restart nginx

1. Verifique la IP y busque el segmento de red
2. Ifconfig ens33: 1 192.168.49.148/24 Agregar ip a la
tarjeta de red
3. Ifconfig vea la tarjeta de red 4. Edite el archivo de configuración /etc/nginx/nginx.conf Ingrese dos direcciones IP antes del puerto de escucha
5. Vuelva a cargar el archivo de configuración de nginx (reinicie nginx si hay un caché (systemctl restart nginx))
6. Visite ip curl 192.168.49.144
curl 192.168.49.148 (IP agregada)

3. Host virtual basado en puerto

Una IP

[root@localhost ~]# cat /etc/nginx/nginx.conf
user  root;
worker_processes  4;

worker_rlimit_nofile 102400;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    sendfile        on;

    keepalive_timeout  65;


    server {
    
    
        listen       80;
        server_name  web.testpm.com;
        location / {
    
    
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate	2k;
        }
        
    
     server {
    
    
        listen       8080;
        server_name  web.1000phone.com;
        location / {
    
    
            root   /1000phone/html/;
            index  index.html index.htm;
        	}
        }
}

Vuelva a cargar el archivo de configuración:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

Visita de prueba:
entrada del navegador: http://web.testpm.com/
entrada del navegador: http://web.1000phone.com:8080

Supongo que te gusta

Origin blog.csdn.net/weixin_49844466/article/details/108287524
Recomendado
Clasificación