赞
踩
目录
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。
nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
2、基于ip的虚拟主机, (一台主机绑定多个ip地址)
3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)
1.查看nginx是否启动
2.在/usr/share/nginx/html/下创建aaa,bbb,ccc三个目录并在aaa,bbb,ccc下分别创建index.html文件并写入不同的内容用来测试虚拟主机是否部署成功
vim /etc/nginx/conf.d/nginx.conf(若没有自己创建)
- server {
- listen 80; #端口
- server_name localhost; #域名
- location /
- {
- root /usr/share/nginx/html/aaa;
- index index.html index.htm;
- }
- }
-
- server {
- listen 90;
- server_name localhost;
- location /
- {
- root /usr/share/nginx/html/bbb;
- index index.html index.htm;
- }
- }
-
- server {
- listen 100;
- server_name localhost;
- location /
- {
- root /usr/share/nginx/html/ccc;
- index index.html index.htm;
- }
- }

浏览器访问IP:80 IP:90 IP:100
若分别出现aaa,bbb,ccc下index.html的内容为部署成功
1.为服务器添加两个IP
ip a a 10.12.153.220/24 dev ens33
ip a a 10.12.153.221/24 dev ens33
- server {
- listen 10.12.153.220:80; #端口
- server_name localhost; #域名
- location /
- {
- root /usr/share/nginx/html/aaa;
- index index.html index.htm;
- }
- }
-
- server {
- listen 10.12.153.221:80;
- server_name localhost;
- location /
- {
- root /usr/share/nginx/html/bbb;
- index index.html index.htm;
- }
- }
-
- server {
- listen 10.12.153.222:80;
- server_name localhost;
- location /
- {
- root /usr/share/nginx/html/ccc;
- index index.html index.htm;
- }
- }

浏览器访问10.12.153.220,10.12.153.221,10.12.153.222
若分别出现aaa,bbb,ccc下index.html的内容为部署成功
- server {
- listen 80; #端口
- server_name www.test.com; #域名
- location /
- {
- root /usr/share/nginx/html/aaa;
- index index.html index.htm;
- }
- }
-
- server {
- listen 80;
- server_name www.test1.com;
- location /
- {
- root /usr/share/nginx/html/bbb;
- index index.html index.htm;
- }
- }
-
- server {
- listen 80;
- server_name www.test2.com;
- location /
- {
- root /usr/share/nginx/html/ccc;
- index index.html index.htm;
- }
- }

#注意 使用域名时需本地解析
Windows解析路径
C:\Windows\System32\drivers\etc\hosts
10.12.153.222 www.test.com
10.12.153.222 www.test1.com
10.12.153.222 www.test2.com
浏览器分别访问www.test.com,www.test1.com,www.test2.com
若分别出现aaa,bbb,ccc下index.html的内容为部署成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。