ITPub博客

首页 > 应用开发 > IT综合 > nginx之 nginx虚拟机配置

nginx之 nginx虚拟机配置

原创 IT综合 作者:张冲andy 时间:2017-06-14 20:53:26 0 删除 编辑

 什么是虚拟主机:

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

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
2、基于ip的虚拟主机, (一块主机绑定多个ip地址)
3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

范例:

一、 基于域名的虚拟主机

1、配置通过域名区分的虚拟机
[root@mysql03 nginx]# cat conf/nginx.conf
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name
location / {
root html;
index index.html index.htm;
}
}

server {
listen 80;
server_name
location / {
root /root/html;
index index.html index.htm;
}
}
}

2、 为 域名为 的虚拟机,创建 index 文件    
[root@mysql03 ~]# mkdir -p /root/html
[root@mysql03 ~]# cd /root/html/
[root@mysql03 html]# vi index.html
[root@mysql03 html]# cat index.html 
<html>
<p>
this is my nginx


</html>

3、重新加载配置文件
[root@mysql03 nginx]# ./sbin/nginx -s reload

4、客户端配置路由映射
在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行

10.219.24.26
10.219.24.26
如图:

5、 测试访问

浏览器输入:http:///

浏览器输入:http:///

 >成功!

 补充:如果配置不能正常访问, 试参考 http://blog.csdn.NET/zhang123456456/article/details/73252148

二、 基于ip的虚拟主机

1. 一块网卡绑定多个ip
[root@mysql03 nginx]# ifconfig eth0:1 10.219.24.27 
[root@mysql03 nginx]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02 
inet addr:10.219.24.26 Bcast:10.255.255.255 Mask:255.0.0.0
...
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02 
inet addr:10.219.24.27 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
2. 配置通过ip区分的虚拟机 
[root@mysql03 nginx]# cat conf/nginx.conf
user root root; #说明:这里的user根据 自己的nginx.conf文件所在的目录的属主属性而定 
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 10.219.24.26:80;
server_name
location / {
root html;
index index.html index.htm;
}
}

server {
listen 10.219.24.27:80;
server_name
location / {
root /root/html;
index index.html index.htm;
}
}
}
3. reopen nginx
[root@mysql03 nginx]# ./sbin/nginx -s reopen

补充:
-- 删除绑定的vip
ifconfig eth0:1 10.219.24.27 down

三、 基于端口的虚拟主机

配置通过端口区分的虚拟机
[root@mysql03 nginx]# cat conf/nginx.conf
user root root; #说明:这里的user根据 自己的nginx.conf文件所在的目录的属主属性而定 
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name
location / {
root html;
index index.html index.htm;
}
}

server {
listen 8080;
server_name
location / {
root /root/html;
index index.html index.htm;
}
}
}

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31383567/viewspace-2140750/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
支持就关注我个人公众号:DBA烂笔头 。专注数据库领域,熟悉oracle、mysql、redis、mongodb,clickhouse等相关技术

注册时间:2016-08-11

  • 博文量
    626
  • 访问量
    286409