博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx之 nginx虚拟机配置
阅读量:2495 次
发布时间:2019-05-11

本文共 2810 字,大约阅读时间需要 9 分钟。

 什么是虚拟主机:

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供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 www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}

server {

listen 80;
server_name www.nginx02.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}

2、 为 域名为 www.nginx02.com 的虚拟机,创建 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 www.nginx01.com

10.219.24.26 www.nginx02.com
如图:

5、 测试访问

浏览器输入:http://www.nginx01.com/

浏览器输入:http://www.nginx02.com/

 >成功!

 补充:如果配置不能正常访问, 试参考 http://blog.csdn/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 www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}

server {

listen 10.219.24.27:80;
server_name www.nginx01.com;
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 www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}

server {

listen 8080;
server_name www.nginx01.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}

 

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

转载于:http://blog.itpub.net/31383567/viewspace-2140750/

你可能感兴趣的文章
mutli-lane情况
查看>>
利用原生js做数据管理平台
查看>>
绘制自定义键盘
查看>>
如何开始DDD(完)
查看>>
poj1969---找规律
查看>>
Linux 复习重点目录
查看>>
[svc]gns3模拟器及探讨几个bgp问题
查看>>
Error:fatal: Not a git repository (or any of the parent directories): .git
查看>>
韩顺平循序渐进学java 第13讲 抽象类.接口
查看>>
Uva489
查看>>
【原创】ABAP动态编程之功能实现
查看>>
java中范型的限定
查看>>
虚拟机中centos安装gcc
查看>>
JAVA中ACTION层, SERVICE层 ,MODLE层 和 DAO层的功能区分
查看>>
动态网页开发基础
查看>>
ATM + 购物商城程序
查看>>
x64 stack walking、调用约定、函数参数识别
查看>>
Hibernate查询
查看>>
(转)swc与swf的区别
查看>>
(转)AS3中的stage,this,root的区别
查看>>