三种虚拟只记得配置方法"/>
Nginx三种虚拟只记得配置方法
一、基于端口配置
先在/data下创建三个目录,往这三个目录写入 "hello,nginx"
[root@localhost data]# mkdir nginx{1..3}
[root@localhost data]# echo "hello,nginx1" > nginx1/index.html
[root@localhost data]# echo "hello,nginx2" > nginx2/index.html
[root@localhost data]# echo "hello,nginx3" > nginx3/index.html
进入/etc/nginx/conf.d/vhost.conf中写入以下内容
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;}
}
写入后用“nginx -t”检查是否写对
[root@localhost data]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
测试
[root@localhost data]# curl localhost:80
hello,nginx1
[root@localhost data]# curl localhost:81
hello,nginx2
[root@localhost data]# curl localhost:82
hello,nginx3
二、基于域名配置
主配置文件
server {listen 192.168.159.134:80;server_name www.nginx1;location / {root /data/nginx1;index index.html;}
}server {listen 192.168.159.134:80;server_name www.nginx2;location / {root /data/nginx2;index index.html;}
}server {listen 192.168.159.134:80;server_name www.nginx1;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.159.134 www.nginx1
192.168.159.134 www.nginx2
192.168.159.134 www.nginx3
测试
[root@localhost etc]# curl www.nginx1
hello,nginx1
[root@localhost etc]# curl www.nginx2
hello,nginx2
[root@localhost etc]# curl www.nginx3
hello,nginx1
三、基于IP地址配置
增加两个IP地址
[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.159.151
[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.159.150
[root@localhost ~]# nmcli connection up ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5)
修改主配置文件
server {listen 192.168.159.134:80;server_name localhost;location / {root /data/nginx1;index index.html;}
}server {listen 192.168.159.150:80;server_name localhost;location / {root /data/nginx2;index index.html;}
}server {listen 192.168.159.151:80;server_name localhost;location / {root /data/nginx3;index index.html;}
}
测试
[root@localhost ~]# curl 192.168.159.151
hello,nginx3
[root@localhost ~]# curl 192.168.159.150
hello,nginx2
[root@localhost ~]# curl 192.168.159.134
hello,nginx1
更多推荐
Nginx三种虚拟只记得配置方法
发布评论