Nginx三种虚拟只记得配置方法

编程入门 行业动态 更新时间:2024-10-22 15:34:28

Nginx<a href=https://www.elefans.com/category/jswz/34/1770022.html style=三种虚拟只记得配置方法"/>

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三种虚拟只记得配置方法

本文发布于:2023-11-15 01:07:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:三种   方法   Nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!