# 虚拟主机
* * * * *
--: 作者:Mick
时间:2019年4月18日
* * * * *
### 配置方式
##### 基于多个域名(host名称)的方式
![](https://box.kancloud.cn/5a9801b16bd018ec5daf4018f59ca5ee_993x528.png)
##### 基于端口号的方式
![](https://box.kancloud.cn/678d073f2a34465b97400ce231d6ea21_990x516.png)
##### 基于多ip的方式
![](https://box.kancloud.cn/b2d83f45a7ea11f128f2bf1d5db30dce_990x523.png)
##### 基于端口的配置演示
修改监听的端口
server {
listen 80;
server_name localhost;
location / {
root html/80;
index index.html;
}
}
server {
listen 81;
server_name localhost;
location / {
root html/81;
index index.html;
}
}
##### 基于域名的配置演示
修改服务名称
server {
listen 80;
server_name one.com;
location / {
root html/one;
index index.html;
}
}
server {
listen 81;
server_name two.com;
location / {
root html/two;
index index.html;
}
}
##### 基于多ip的方式的演示
查看网卡
```
[root@localhost nginx]# ./bin/nginx
[root@localhost nginx]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: **eno16777736**: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d3:64:ed brd ff:ff:ff:ff:ff:ff
inet 192.168.3.194/24 brd 192.168.3.255 scope global dynamic eno16777736
valid_lft 84724sec preferred_lft 84724sec
inet6 fe80::20c:29ff:fed3:64ed/64 scope link
valid_lft forever preferred_lft forever
```
##### 添加网卡
ip a add 192.168.3.195/24 dev eno16777736
##### nginx配置监听ip与端口号
server {
listen 192.168.3.194:80;
server_name localhost;
location / {
root html/194;
index index.html;
}
}
server {
listen 192.168.3.195:80;
server_name localhost;
location / {
root html/195;
index index.html;
}
}