一、虚拟主机概述
1.1 什么是虚拟主机
虚拟主机,就是把一台物理服务器划分成多个 “虚拟” 的服务器,这样我们的一台物理服务器就可以当做多个服务器来使用,从而可以配置多个网站。Nginx 提供虚拟主机的功能,就是为了让我们不需要安装多个 Nginx,就可以运行多个域名不同的网站。
每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。
Nginx 下,一个 server 标签就是一个虚拟主机。nginx 的虚拟主机就是通过主配置文件 nginx.conf 中 server 节点指定的,想要设置多个虚拟主机,配置多个server节点即可。
1.2 配置虚拟主机的方法
配置虚拟主机有三种方法:
- 基于域名的虚拟主机 : 不同的域名、相同的IP(此方式应用最广泛)。
- 基于IP地址的虚拟主机 : 不同的域名、不同的IP ( 需要加网络接口 ,应用的不广泛), 基于IP地址。
- 基于端口的虚拟主机 : 不使用域名、IP来区分不同站点的内容,而是用不同的TCP端口号。
二、基于域名的虚拟主机
2.1 为虚拟主机提供域名解析
[root@localhost ~]# echo "192.168.10.80 www.km.com www.benet.com" >> /etc/hosts
2.2 为虚拟主机准备网页文档
[root@localhost ~]# mkdir -p /var/www/html/benet
[root@localhost ~]# mkdir -p /var/www/html/km
[root@localhost ~]# echo "<h1>www.km.com</h1>" > /var/www/html/km/index.html
[root@localhost ~]# echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
2.3 修改Nginx的配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
----------------------------
server {
listen 80;
server_name localhost;
charset utf-8;
access_log logs/www.km.access.log;
location / {
root /var/www/html/km;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
--------------------------------
2.4 重启服务,访问测试
[root@localhost ~]# nginx -t //检查配置文件的配置项是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
浏览器访问
http://www.km.com
http://www.benet.com
三、基于ip地址的nginx虚拟主机
3.1 设置临时ip,以达到一台服务器拥有多个ip地址,不同ip访问不同的服务页面
[root@localhost ~]# ifconfig ens33:0 192.168.10.90 netmask 255.255.255.0
[root@localhost ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.90 netmask 255.255.255.0 broadcast 192.168.10.255
ether 00:0c:29:8a:f0:28 txqueuelen 1000 (Ethernet)
3.2 修改配置文件,之后重启服务,访问测试。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
--------------------------
server {
listen 192.168.10.80:80; #设置监听地址192.168.10.19
server_name www.km.com;
charset utf-8;
access_log logs/www.km.access.log;
location / {
root /var/www/html/kgc;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.10.90:80; #设置监听地址192.168.10.40
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
-------------------------------
3.3 重启服务,访问测试
[root@localhost ~]# nginx -t //检查配置文件的配置项是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
浏览器访问
http://192.168.10.80
http://192.168.10.90
四、基于端口的nginx虚拟主机
修改IP地址后面的端口即可。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
-----------------------------
server {
listen 192.168.10.80:8080; #设置监听 8080 端口
server_name www.km.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /var/www/html/km;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.10.80:8888; #设置监听 8888 端口
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
浏览器访问
http://192.168.10.80:8080
http://192.168.10.80:8888