一个web服务器软件默认情况下只能发布一个web,因为一个web分享出去需要三个条件(IP、Port、Domain name)
一个web服务器软件如何发布多个web呢?
虚拟主机:
就是把一台物理服务器划分成多个"虚拟"的服务器,每一个虚拟主机都可以有独立的域名和独立的目录
1)基于IP的虚拟主机
2)基于端口的虚拟主机
3)基于域名的虚拟主机
现在同时发布两个网站:
DocumentRoot /usr/local/nginx/html/web1
DocumentRoot /usr/local/nginx/html/web2
sed -i “/#/d” nginx.conf
sed -i “/^$/d” nginx.conf
上面两条命令过滤掉nginx.conf中带有#和空行的
基于IP–每个网站需要一个IP
缺点:如果是公网ip,那每个ip需要付费
1)需要两个IP
2)保证DocumentRoot存在
3)索引页 index.html
server {
listen 192.168.43.129:80;
server_name localhost;
location / {
root html/web1;
index index.html index.htm;
}
}
server {
listen 192.168.43.130:80;
server_name localhost;
location / {
root html/web2;
index index.html index.htm;
}
}
创建IP地址
使用逻辑子网卡设置:
ifconfig eno16777736:1 192.168.43.129/24 up
ifconfig eno16777736:2 192.168.43.130/24 up
创建目录
mkdir /usr/local/nginx/html/web1
mkdir /usr/local/nginx/html/web2
创建索引页
echo web1 > /usr/local/nginx/html/web1/index.html
echo web2 > /usr/local/nginx/html/web2/index.html
测试
基于端口–只需要一个IP
缺点:端口是无法告诉公网用户 无法适用于公网用户 适合内部用户
server {
listen 80;
location / {
root html/web1;
index index.html index.htm;
}
}
server {
listen 8080;
location / {
root html/web2;
index index.html index.htm;
}
}
测试
基于域名
一个网站必然有一个域名
解决了IP和端口的问题
server {
listen 80;
server_name www.abc.com
location / {
root html/web1;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.cbd.com
location / {
root html/web2;
index index.html index.htm;
}
}
修改/etc/hosts 文件
添加域名解析:
192.168.43.128 www.abc.com
192.168.43.128 www.cbd.com
测试