一台虚拟主机运行多个网站的常用方式
- 多IP
- 多端口
- 多域名
1、多IP的方式
- 需要主机上有多个网卡,多个IP地址
- 将网站IP绑定在指定的网卡
listen 192.168.200.110:80;
listen 192.168.200.100:80;
[root@nginx ~]# vim /etc/nginx/conf.d/ip_01.conf
server {
listen 192.168.200.120:80;
location / {
root /code/ip_01;
index index.html;
}
}
[root@nginx ~]# vim /etc/nginx/conf.d/ip_02.conf
server {
listen 192.168.200.121:80;
location / {
root /code/ip_02;
index index.html;
}
}
[root@nginx ~]# mkdir /code/ip_01
[root@nginx ~]# echo "test_ip_01" > /code/ip_01/index.html
[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx ~]# systemctl reload nginx.service
2、多端口的方式
[root@nginx ~]# vim /etc/nginx/conf.d/port-8080.conf
server {
listen 8080;
server_name locahost;
location / {
root /code/port-8080;
index index.html;
}
}
[root@nginx ~]# vim /etc/nginx/conf.d/port-80.conf
server {
listen 80;
server_name locahost;
location / {
root /code/port-80;
index index.html;
}
}
3、多域名的方式
[root@nginx ~]# vim /etc/nginx/conf.d/test-01.conf
server {
listen 80;
server_name www.test-01.org;
location / {
root /code/test-01;
index index.html;
}
}
[root@nginx ~]# vim /etc/nginx/conf.d/test-02.conf
server {
listen 80;
server_name www.test-02.org;
location / {
root /code/test-02;
index index.html;
}
}