首页 > 网站 > Nginx > 正文

详解Nginx 虚拟主机配置的三种方式(基于IP)

2024-08-30 12:24:44
字体:
来源:转载
供稿:网友

Nginx配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。

详解Nginx 虚拟主机配置的三种方式(基于端口) https://www.Vevb.com/article/14977.htm

详解Nginx 虚拟主机配置的三种方式(基于域名) https://www.Vevb.com/article/14978.htm

1、基于IP的虚拟主机配置

如果同一台服务器有多个IP,可以使用基于IP的虚机主机配置,将不同的服务绑定在不同的IP上。

1.1 假设服务器有个IP地址为192.168.2.150,首先使用ifconfig在同一个网络接口上绑定其他3个IP。

[root@localhost ~]# ifconfig ens33:1 192.168.2.151/24 up[root@localhost ~]# ifconfig ens33:2 192.168.2.152/24 up[root@localhost ~]# ifconfig ens33:3 192.168.2.153/24 up[root@localhost ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.106 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::2a8d:be6:a4a8:ea0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) RX packets 1220 bytes 87955 (85.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 206 bytes 23755 (23.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.151 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.152 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)ens33:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.153 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 72 bytes 6252 (6.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 72 bytes 6252 (6.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

1.2 3个IP对应的域名如下,配置主机的host文件便于测试

[root@localhost ~]# vim /etc/hosts[root@localhost ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1  localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.2.151 www.test151.com192.168.2.152 www.test152.com192.168.2.153 www.test153.com

可以模拟实现DNS轮询的情况。

附:设置完hosts文件后一定要记得执行以下命令使其生效

1、windows下cmd进入命令行

C:/Users/1234>ipconfig /flushdnsWindows IP 配置已成功刷新 DNS 解析缓存。

1.3 建立虚拟主机存放网页的根目录,并创建首页文件index.html

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表