什么是虚拟主机
虚拟主机是一种特殊的软硬件技术,他可以将网络上每一台计算机分成多台虚拟主机,
每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,
每个虚拟主机之间是独立的,互不影响的
Nginx虚拟主机配置
Nginx配置解析的三种方式:
通过nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置
1> 基于ip的虚拟主机(一块主机绑定多个ip地址)
2> 基于域名的虚拟主机(servername)
3> 基于端口的虚拟主机(listen如果不写ip端口模式)
例如:基于虚拟机ip的配置,这里需要配置多个ip
server
{
listen 192.168.20.20:80;
server_name www.linuxidc.com;
root /data/www;
}
server
{
listen 192.168.20.21:80;
server_name www.linuxidc.com;
root /data/www;
}
1、基于域名的虚拟主机
1.1、进入tengine目录
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll
1.2、更改nginx.conf文件
[root@node1 conf]# vi nginx.conf
server {
listen 80;
server_name www.matrix.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.matrix2.com;
location / {
root /opt/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
1.3、配置第二个虚拟主机的访问页面
[root@node1 conf]# cd /opt
[root@node1 opt]# mkdir html
[root@node1 opt]# cd html
将<html><h1>Hello World!</h1></html>内容复制到index.html文件中
[root@node1 html]# vim index.html
1.4、重新nginx服务
[root@node1 html]# service nginx restart
1.5、在window上进行ip地址映射【配置路由表】
【编辑C:\Windows\System32\drivers\etc\hosts文件】
192.168.230.10 www.matrix.com
192.168.230.10 www.matrix2.com
1.6、查看配置是否成功
在地址栏输入:www.matrix.com
在地址栏输入:www.matrix2.com
2、基于ip的虚拟主机
2.1、进入tengine目录
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll
2.2、更改nginx.conf文件
一个主机对应多个ip地址
[root@node1 conf]# vi nginx.conf
server {
listen 192.168.230.10:80;
server_name www.sparsematrix.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.230.18:80;
server_name www.sparsematrix2.com;
location / {
root /opt/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2.3、在Linux主机中设置虚拟ip
[root@node1 html]# ifconfig eth0:1 192.168.230.18 netmask 255.255.255.0
2.4、重启nginx服务
[root@node1 html]# service nginx restart
2.5、在window上进行ip地址映射
【编辑C:\Windows\System32\drivers\etc\hosts文件】
192.168.230.10 www.sparsematrix.com
192.168.230.18 www.sparsematrix.com
2.6、查看配置是否成功
在地址栏输入:http://http://192.168.230.10/
在地址栏输入:http://http://192.168.230.18/
3、基于端口的虚拟主机
3.1、进入tengine目录
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll
3.2、更改nginx.conf文件
[root@node1 conf]# vi nginx.conf
server {
listen 192.168.230.10:80;
server_name www.sparsematrix.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.230.10:8088;
server_name www.sparsematrix.com;
location / {
root /opt/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3.4、重启nginx服务
[root@node1 html]# service nginx restart
3.5、在window上进行ip地址映射
【编辑C:\Windows\System32\drivers\etc\hosts文件】
192.168.230.10 www.sparsematrix.com
3.6、查看配置是否成功
在地址栏输入:http://http://192.168.230.10/
在地址栏输入:http://192.168.230.10:8088/