nginx虚拟主机配置
在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器会同时配置N个虚拟主机,这样可以充分利用服务器的资源,方便管理员的统一管理
配置nginx虚拟主机首先肯定要先部署咱们的nginx,具体nginx部署安装请移步 nginx部署,配置nginx虚拟主机需要大家了解nginx配置文件以及nginx的各个目录,若需要请移步 nginx目录结构与配置文件详解
配置nginx虚拟主机有三种方法:基于ip地址的虚拟主机、基于域名的虚拟主机以及基于端口的虚拟主机,下面为大家逐一讲解
基于域名的虚拟主机
基于域名的虚拟主机原理:相同IP地址,相同端口、不同的域名。也就是说多个虚拟主机之间共用一个ip地址以及一个端口(80),区分各个主机之间使用不同的域名,当然访问的时候也就只能使用域名进行访问了,基于域名的虚拟主机是最常用的方式
配置:
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)去访问了
配置
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地址,但不可一个域名绑定多个ip
配置
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; } ......省略其他代码 } }
实战
我这里为大家演示一种虚拟主机的实现,因为基于域名的虚拟主机比较常见这里就为大家演示这种虚拟主机
环境介绍
操作系统版本:
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版本:
root@jia:~# nginx -v nginx version: nginx/1.16.1 (Ubuntu)
配置nginx.conf
配置文件位置:/etc/nginx/conf.d/*.conf
nginx安装方式不同位置不同,具体请查看自己的配置文件位置
下面是配置文件内容:
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