Nginx虚拟主机
准备实验环境
1)创建站点目录
[root@www ~]# for i in web1 web2
> do
> mkdir /usr/share/nginx/html/$i
> done
[root@www ~]# ll /usr/share/nginx/html/
total 8
-rw-r--r-- 1 root root 494 May 25 2021 50x.html
-rw-r--r-- 1 root root 612 May 25 2021 index.html
drwxr-xr-x 2 root root 6 Jun 11 18:08 web1
drwxr-xr-x 2 root root 6 Jun 11 18:08 web2
2)创建主页文件
[root@www ~]# for i in web1 web2
> do
> echo "this is $i test page" > /usr/share/nginx/html/$i/index.html
> done
[root@www ~]# for i in web1 web2
> do
> ll /usr/share/nginx/html/$i/index.html
> done
-rw-r--r-- 1 root root 23 Jun 11 18:11 /usr/share/nginx/html/web1/index.html
-rw-r--r-- 1 root root 23 Jun 11 18:11 /usr/share/nginx/html/web2/index.html
3)创建虚拟主机配置文件
[root@www ~]# touch /etc/nginx/conf.d/vhost.conf
1、基于IP
1)修改虚拟主机配置文件
[root@www ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 192.168.25.151:80;
server_name web1.test.com;
location / {
root /usr/share/nginx/html/web1;
index index.html index.htm;
}
}
server {
listen 192.168.25.152:80;
server_name web2.test.com;
location / {
root /usr/share/nginx/html/web2;
index index.html index.htm;
}
}
2)为网卡添加IP地址
[root@www ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens33 be90387d-bd23-4ab4-944c-cbd722d5c952 ethernet ens33
[root@www ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.25.151/24
[root@www ~]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@www ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.25.152/24
[root@www ~]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@www ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:31:c9:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.25.137/24 brd 192.168.25.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.25.151/24 brd 192.168.25.255 scope global secondary noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.25.152/24 brd 192.168.25.255 scope global secondary noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::8fed:5970:dfc6:462b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3)可以通过不同IP测试访问
正常测试(使用curl):
[root@localhost ~]# curl 192.168.25.151
this is web1 test page
[root@localhost ~]# curl 192.168.25.152
this is web2 test page
浏览器测试:
安装浏览器elinks:
[root@localhost ~]# yum install elinks -y
[root@localhost ~]# elinks 192.168.25.151
[root@localhost ~]# elinks 192.168.25.152
浏览器测试结果:
web1
web2
2、基于端口
1)修改虚拟主机配置文件
[root@www ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 81;
server_name web1.test.com;
location / {
root /usr/share/nginx/html/web1;
index index.html index.htm;
}
}
server {
listen 82;
server_name web2.test.com;
location / {
root /usr/share/nginx/html/web2;
index index.html index.htm;
}
}
2)在另一台设备上配置hosts域名信息
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.25.137 web1.test.com web2.test.com
3)可以通过相同地址不同端口测试访问
正常测试(使用curl):
[root@localhost ~]# curl 192.168.25.137:81
this is web1 test page
[root@localhost ~]# curl 192.168.25.137:82
this is web2 test page
浏览器测试:
安装浏览器elinks:
[root@localhost ~]# yum install elinks -y
[root@localhost ~]# elinks 192.168.25.137:81
[root@localhost ~]# elinks 192.168.25.137:82
浏览器测试结果:
web1
web2
3、基于域名
1)修改虚拟主机配置文件
[root@www ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name web1.test.com;
location / {
root /usr/share/nginx/html/web1;
index index.html index.htm;
}
}
server {
listen 80;
server_name web2.test.com;
location / {
root /usr/share/nginx/html/web2;
index index.html index.htm;
}
}
2)在另一台设备上配置hosts域名信息
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.25.137 web1.test.com web2.test.com
3)可以通过不同域名测试访问
正常测试(使用curl):
[root@localhost ~]# curl web1.test.com
this is web1 test page
[root@localhost ~]# curl web2.test.com
this is web2 test page
浏览器测试:
安装浏览器elinks:
[root@localhost ~]# yum install elinks -y
[root@localhost ~]# elinks web1.test.com
[root@localhost ~]# elinks web2.test.com
浏览器测试结果:
web1
web2