Nginx虚拟主机配置

发布 : 2016-02-22 分类 : 大数据 浏览 :

什么是虚拟主机

1
2
3
虚拟主机是一种特殊的软硬件技术,他可以将网络上每一台计算机分成多台虚拟主机,
每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,
每个虚拟主机之间是独立的,互不影响的

Nginx虚拟主机配置

Nginx配置解析的三种方式:

1
2
3
4
5
通过nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置

1> 基于ip的虚拟主机(一块主机绑定多个ip地址)
2> 基于域名的虚拟主机(servername)
3> 基于端口的虚拟主机(listen如果不写ip端口模式)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
例如:基于虚拟机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目录

1
2
3
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll
`

1.2.更改nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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.配置第二个虚拟主机的访问页面

1
2
3
4
5
6
7
8
9
[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服务

1
[root@node1 html]# service nginx restart

1.5.在window上进行ip地址映射【配置路由表】

1
2
3
【编辑C:\Windows\System32\drivers\etc\hosts文件】
192.168.230.10 www.matrix.com
192.168.230.10 www.matrix2.com

1.6.查看配置是否成功

1
在地址栏输入:www.matrix.com

1
在地址栏输入:www.matrix2.com

2.基于ip的虚拟主机

2.1.进入tengine目录

1
2
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll

2.2.更改nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
一个主机对应多个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.sparsematrix.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

1
[root@node1 html]# ifconfig eth0:1 192.168.230.18 netmask 255.255.255.0

2.4.重启nginx服务

1
[root@node1 html]# service nginx restart

2.5.在window上进行ip地址映射

1
2
3
【编辑C:\Windows\System32\drivers\etc\hosts文件】
192.168.230.10 www.sparsematrix.com
192.168.230.18 www.sparsematrix.com

2.6.查看配置是否成功

1
在地址栏输入:http://http://192.168.230.10/

1
在地址栏输入:http://http://192.168.230.18/

3.基于端口的虚拟主机

3.1.进入tengine目录

1
2
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll

3.2.更改nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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.3.重启nginx服务

1
[root@node1 html]# service nginx restart

3.4.在window上进行ip地址映射

1
2
【编辑C:\Windows\System32\drivers\etc\hosts文件】
192.168.230.10 www.sparsematrix.com

3.5.查看配置是否成功

1
在地址栏输入:http://http://192.168.230.10/

1
在地址栏输入:http://192.168.230.10:8088/

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2016/02/22/Nginx虚拟主机配置/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹