基于IP的配置
首先在原本基础上增加两个IP地址
[root@localhost conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.38.140
[root@localhost conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.38.150
[root@localhost conf.d]# nmcli connection up ens33
然后配置虚拟主机
在/etc/nginx/conf.d/vhost.conf
server {
listen 192.168.38.135:80;
server_name localhost;
location / {
root /data/nginx1;
index index.html;
}
}
server {
listen 192.168.38.140:80;
server_name localhost;
location / {
root /data/nginx2;
index index.html;
}
}
server {
listen 192.168.38.150:80;
server_name localhost;
location / {
root /data/nginx3;
index index.html;
}
}
验证
基于端口的虚拟主机
直接配置
server {
listen 80;
server_name localhost;
location / {
root /data/nginx1;
index index.html;
}
}
server {
listen 81;
server_name localhost;
location / {
root /data/nginx2;
index index.html;
}
}
server {
listen 82;
server_name localhost;
location / {
root /data/nginx3;
index index.html;
}
}
验证
基于域名的虚拟主机
虚拟主机的配置
server {
listen 192.168.38.135:80;
server_name www.nginx1.com;
location / {
root /data/nginx1;
index index.html;
}
}
server {
listen 192.168.38.135:80;
server_name www.nginx2.com;
location / {
root /data/nginx2;
index index.html;
}
}
server {
listen 192.168.38.135:80;
server_name www.nginx1.com;
location / {
root /data/nginx3;
index index.html;
}
}
因为我们只是在虚拟机上测试,在测试机对hosts文件进行配置
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.38.135 www.nginx1.com
192.168.38.135 www.nginx2.com
192.168.38.135 www.nginx3.com
~
验证