Nginx虚拟机配置详解

栏目: 服务器 · Nginx · 发布时间: 6年前

内容简介:Nginx虚拟机配置详解

什么是虚拟主机:

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

Nginx虚拟机配置详解

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。

1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)

2、基于ip的虚拟主机, (一块主机绑定多个ip地址)

3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

范例:

一、 基于域名的虚拟主机

1、配置通过域名区分的虚拟机

[root@linuxidc.com 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@linuxidc.com ~]# mkdir -p /root/html

[root@linuxidc.com ~]# cd /root/html/

[root@linuxidc.com html]# vi index.html

[root@linuxidc.com html]# cat index.html

<html>

<p>

this is my nginx

</p>

</html>

3、重新加载配置文件

[root@linuxidc.com 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

如图:

Nginx虚拟机配置详解

5、 测试访问

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

Nginx虚拟机配置详解

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

Nginx虚拟机配置详解

>成功!

补充:如果配置不能正常访问, 试参考 http://www.linuxidc.com/Linux/2017-06/144827.htm

二、 基于ip的虚拟主机

1. 一块网卡绑定多个ip

[root@linuxidc.com nginx]# ifconfig eth0:1 10.219.24.27

[root@linuxidc.com 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@linuxidc.com 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@linuxidc.com nginx]# ./sbin/nginx -s reopen

补充:

-- 删除绑定的vip

ifconfig eth0:1 10.219.24.27 down

三、 基于端口的虚拟主机

配置通过端口区分的虚拟机

[root@linuxidc.com 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;

}

}

}

CentOS 7下Nginx服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS上安装Nginx服务器实现虚拟主机和域名重定向 http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装LNMP环境(Linux+Nginx+MySQL+PHP) http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux下安装 PHP 环境并配置Nginx支持php-fpm模块 http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx服务的SSL认证和htpasswd认证 http://www.linuxidc.com/Linux/2017-04/142478.htm

Linux中安装配置Nginx及参数详解 http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2下Nginx+PHP+MySQL+Memcache缓存服务器安装配置 http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9编译安装Nginx1.4.7 http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍 请点这里

Nginx 的下载地址 请点这里

本文永久更新链接地址 http://www.linuxidc.com/Linux/2017-06/144826.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

四步创业法

四步创业法

[美] Steven Gary Blank / 七印部落 / 华中科技大学出版社 / 2012-8-1 / 48.00

《四步创业法》获李开复推荐,是精益创业理论的奠基之作。作者Steven Gary Blank博士是硅谷资深企业家,他一共创办了八家企业,并担任多家硅谷公司的董事和创业顾问。本书总结作者25年创业经验,提出全新的客户发展方法 (诣在弥补传统产品开发方法的缺陷) ,掀起了硅谷近年精益创业 的浪潮。七印部落正在翻译作者的博客和授课视频,欢迎访问微博:http://weibo.com/7seals ......一起来看看 《四步创业法》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具