使用 Nginx 搭建虚拟机服务器时,每个虚拟web 站点拥有独立的 “server { }” 配置端,各自监听的IP地址、端口号可以单独制定,当然网站名称也是不同的。
一、基于域名:
(1)环境准备:
①、这两个域名指向同一个IP地址
IP地址 | 域名 |
---|---|
192.168.220.131 | www.kgc.com,www.accp.com |
②、准备每个网站目录和测试首页
(2)修改配置文件 nginx.conf
1、把 server{ } 代码段全部去掉,加入两个新的server{} 段,对应2个域名。
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、使用 nginx -t 检测文件语法是否正确
3、用客户机访问两个域名测试
二、基于端口:
选择系统中不同的端口,将多个端口映射到同一个 IP地址
先修改测试网页:echo “this is kgc8080 web” > kgc/index.html
1、还是修改 nginx.conf 文件
server {
listen 192.168.220.131:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.220.131:8080;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、重启nginx
service nginx stop
service nginx start
3、客户端使用不同端口登录网址
三、基于IP地址:
域名 | IP地址 |
---|---|
www.kgc.com | 192.168.220.131 |
www.accp.com | 192.168.220.142 |
1、修改 nginx.conf 文件
server {
listen 192.168.220.131:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.220.142:80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、客户机验证