在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器会同时配置N个虚拟主机,这样能够充分利用服务器的资源,方便管理员的统一管理html
配置nginx虚拟主机首先确定要先部署我们的nginx,具体nginx部署安装请移步 nginx部署,配置nginx虚拟主机须要你们了解nginx配置文件以及nginx的各个目录,若须要请移步 nginx目录结构与配置文件详解linux
配置nginx虚拟主机有三种方法:基于ip地址的虚拟主机、基于域名的虚拟主机以及基于端口的虚拟主机,下面为你们逐一讲解nginx
基于域名的虚拟主机原理:相同IP地址,相同端口、不一样的域名。也就是说多个虚拟主机之间共用一个ip地址以及一个端口(80),区分各个主机之间使用不一样的域名,固然访问的时候也就只能使用域名进行访问了,基于域名的虚拟主机是最经常使用的方式
配置:web
http{ ......省略其余代码 #第一台虚拟主机 server { listen 80; server_name a.jkyst.xyz; #这里域名必定不要重复 access_log logs/a.access.log; #日志需求能够根据本身的要求去作,若是以为日志无所谓分不分开大能够放到一块儿 location{ root html/a; #这里是网站的根目录,注意为了测试必定要分开,里面写上不一样的html index index.html index.htm; } ......这里省略其余代码 } #第二台虚拟主机 server{ listen 80; server_name b.jkyst.xyz; access_log logs/b.access.log; location{ root html/b; index index.html index.htm; } ......这里省略其余代码 } #第三台虚拟主机 server{ listen 80; server_name c.jkyst.xyz; access_log logs/c.access.log; location{ root html/c; index index.html index.htm; } } }
基于端口的虚拟主机原理:相同IP地址,相同域名,不一样的端口;也就是说多个虚拟主机之间拥有相同的IP地址和域名,使用端口不一样的方式区分不一样虚拟主机,固然访问的时候就不可使用默认的端口(80)去访问了
配置ubuntu
http{ ......省略其余代码 #第一台虚拟主机 server { listen 8000; server_name www.jkyst.xyz; access_log logs/a.access.log; location{ root html/a; index index.html index.htm; } ......省略其余代码 } #第二台虚拟主机 server{ listen 8001; server_name www.jkyst.xyz; access_log logs/b.access.log; location{ root html/b; index index.html index.htm; } ......省略其余代码 } #第三台虚拟主机 server{ listen 8002; server_name www.jkyst.xyz; access_log logs/c.access.log; location{ root html/c; index index.html index.htm; } ......省略其余代码 } }
基于IP地址的虚拟主机原理:相同端口,相同域名,不一样的IP地址;也就是说多个虚拟主机之间拥有相同的端口和域名,使用IP地址不一样的方式区分不一样虚拟主机,固然访问的时候就须要使用不一样IP或者域名去访问了,绑定域名时也就须要不一样域名绑定不一样ip地址,但不可一个域名绑定多个ip
配置windows
http{ ......省略其余代码 #第一台虚拟主机 server { listen IP地址:80; server_name a.jkyst.xy或者ip地址; access_log logs/a.access.log; location{ root html/a; index index.html index.htm; } ......省略其余代码 } #第二台虚拟主机 server{ listen IP地址:80; server_name b.jkyst.xyz或者IP地址; access_log logs/b.access.log; location{ root html/b; index index.html index.htm; } ......省略其余代码 } #第三台虚拟主机 server{ listen IP地址:80; server_name c.jkyst.xyz或者IP地址; access_log logs/c.access.log; location{ root html/c; index index.html index.htm; } ......省略其余代码 } }
我这里为你们演示一种虚拟主机的实现,由于基于域名的虚拟主机比较常见这里就为你们演示这种虚拟主机bash
操做系统版本:服务器
root@jia:~# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 19.10 Release: 19.10 Codename: eoan root@jia:~# cat /proc/version Linux version 5.3.0-18-generic (buildd@lcy01-amd64-027) (gcc version 9.2.1 20190909 (Ubuntu 9.2.1-8ubuntu1)) #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019
nginx版本:curl
root@jia:~# nginx -v nginx version: nginx/1.16.1 (Ubuntu)
配置文件位置:/etc/nginx/conf.d/*.conf
nginx安装方式不一样位置不一样,具体请查看本身的配置文件位置
下面是配置文件内容:ide
root@jia:/var/www/html# cat /etc/nginx/conf.d/default.conf server { listen 80; server_name a.jkyst.xyz; location / { root /var/www/html/a; index index.html index.htm; } } server { listen 80; server_name b.jkyst.xyz; location / { root /var/www/html/b; index index.html index.htm; } } server { listen 80; server_name c.jkyst.xyz; location / { root /var/www/html/c; index index.html index.htm; }
记住修改配置文件后必定要从新启动nginx
root@jia:~# systemctl restart nginx
首先建立存放主页html文件的目录
root@jia:~# cd /var/www/html/ root@jia:/var/www/html# mkdir a b c #生成主页文件 root@jia:/var/www/html# echo a.jkyst.xyz > a/index.html root@jia:/var/www/html# echo b.jkyst.xyz > b/index.html root@jia:/var/www/html# echo c.jkyst.xyz > c/index.html
这里修改hosts文件须要注意一下,在那个PC上面验证就在那个PC上面修改host文件
windows主机hosts文件路径:C:\Windows\System32\drivers\etc
linux版本hosts文件路径:/etc/
须要写入的内容
127.0.0.1 a.jkyst.xyz //127.0.0.1是nginx服务器的IP地址,我这里使用的是同一台PC 127.0.0.1 b.jkyst.xyz 127.0.0.1 c.jkyst.xyz
root@jia:~# curl a.jkyst.xyz a.jkyst.xyz root@jia:~# curl b.jkyst.xyz b.jkyst.xyz root@jia:~# curl c.jkyst.xyz c.jkyst.xyz
OK发现访问的都是我想要获得的域名,证实基于域名的虚拟主机配置成功