nginx虚拟主机配置
2016-03-02 21:21
1121 查看
什么是虚拟主机
虚拟主机是一种特殊的软硬件技术,他可以将网络上每一台计算机分成多台虚拟主机, 每个虚拟主机可以独立对外提供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.confserver { 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.02.4、重启nginx服务
[root@node1 html]# service nginx restart2.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/
相关文章推荐
- Nginx安装
- nginx的常用命令
- win7系统下安装、配置、测试nginx
- PHP脚本监控Nginx 502错误并自动重启php-fpm
- nginx+php5.6.12+discuz
- 使用Nginx、Nginx Plus抵御DDOS攻击
- nginx下配置多站点
- Nginx支持比Apache高并发的原因
- Nginx中虚拟主机与指定访问路径的设置方法讲解
- Nginx Upstream Keepalive 分析
- Nginx的负载均衡
- OS X中多PHP环境与Nginx配置问题
- CDN调度器HAProxy、Nginx、Varnish
- redhat nginx 启动脚本
- Nginx 隐藏服务器名称和版本号
- Ubuntu下安装nginx常见问题及解决办法
- nginx 安装手记
- nginx 跳转指定接口
- 文章标题
- nginx+ffmpeg搭建rtmp转播rtsp流的flash服务器