02 Nginx虚拟主机

01 基于域名实现多任务

# 创建两个Nginx的conf文件
[root@likexy-nginx conf.d]# cat test01.conf 
server{
	listen 80;
	server_name www.test01.com;

	location / {
		root /code;
		index test01.html;
	}
}
[root@likexy-nginx conf.d]# cat test02.conf 
server{
	listen 80;
	server_name www.test02.com;

	location / {
		root /code;
		index test02.html;
	}
}
[root@likexy-nginx conf.d]# cd /code/
[root@likexy-nginx code]# cat test01.html 
test01
[root@likexy-nginx code]# cat test02.html 
test02
# 修改 C:\Windows\System32\drivers\etc\hosts
172.2.25.10	www.test01.com www.test02.com
image-20241124171430150
图1 在浏览器访问是test01
image-20241124171446623
图2 在浏览器访问是test02

02 Nginx基于多IP地址

02-1 多端口

[root@likexy-nginx conf.d]# cat test01.conf 
server{
	listen 80;
	server_name _;

	location / {
		root /code;
		index test01.html;
	}
}
[root@likexy-nginx conf.d]# cat test02.conf 
server{
	listen 81;
	server_name _;

	location / {
		root /code;
		index test02.html;
	}
}

image-20241124172122113

图3 在浏览器访问是test01

image-20241124172206745

图4 在浏览器访问是test02

02-2 多IP

# 添加网卡IP地址
[root@likexy-nginx conf.d]# nmcli c modify ens33 +ipv4.addresses 172.2.25.110/24[root@likexy-nginx conf.d]# nmcli c down ens33 && nmcli c up ens33 
成功停用连接 "ens33"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/1)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)

image-20241124172702227

图5 查看ens33网卡IP地址
# 两个Nginx的conf文件监听端口不一样
[root@likexy-nginx conf.d]# cat test01.conf 
server{
	listen 172.2.25.10:80;
	server_name _;

	location / {
		root /code;
		index test01.html;
	}
}
[root@likexy-nginx conf.d]# cat test02.conf 
server{
	listen 172.2.25.110:80;
	server_name _;

	location / {
		root /code;
		index test02.html;
	}

image-20241124173127449
图6 在浏览器访问是test01
image-20241124173320727
图7 在浏览器访问是test02
posted @ 2025-05-18 17:36  Chains_1  阅读(7)  评论(0)    收藏  举报