1、虚拟主机的概念和类型

  1.1 概念:

    所谓的虚拟主机,在web服务里面就是一个独立的网站站点,这个站点对应独立的域名(也有可能是IP或者端口),具有独立的程序和资源目录,可以独立的对外提供服务。

  1.2 类型:

    基于域名的虚拟主机:用不同的域名区分不同的虚拟主机(或者对外提供的资源)

    基于端口的虚拟主机:用相同的域名不同的端口区分不同的虚拟主机(或者对外提供的资源)

    基于IP的虚拟主机:用不同的IP区分不同的虚拟主机(或者对外提供的资源),在生产中很少见的

    三种类型可以独立使用,也能混合使用,根据需求来确定使用的形式

2、基于域名的虚拟主机

  2.1 修改配置文件:

    我们要对nginx的主配置文件进行修改,在修改之前我们要对nginx.conf 文件进行格式化(这样提取出我们想要的文件,便于更好的修改),格式化还是去除空行和注释的行    

[root@Nginx /]# cd /opt/nginx/conf/      # 切换到配置文件目录
[root@Nginx conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf # 去掉空行和注释 并生成新的文件nginx.conf

    新文件的原始内容

[root@Nginx conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

    我们只需要修改server区块的内容即可,修改后的内容如下:(红色标记修改内容)

[root@Nginx conf]# vim nginx.conf
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {                      # www.brian.com的server区块
listen 80;
server_name www.brian.com;     # 域名
location / {  
root html/brian;            # 站点根目录
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}
}

   这里我们也可以通过添加多个server区块来配置基于多域名的虚拟主机,具体修改nginx.conf文件 如下:(红色为修改内容)

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server { # www.brian.com区块开始
listen 80;
server_name www.brian.com; # 域名
location / {
root html/brian; # 站点根目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }                      # server区块结束
server {                 # www.brianzjz.com 区块开始
listen 80;
server_name www.brianzjz.com; # 域名
location / {
root html/brianzjz; # 站点根目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}                      #server区块结束 }
} 

    配置文件修改完成,接下来来创建上面指定的站点根目录和首页文件

  2.2 创建对应站点的目录和文件:

    创建站点根目录,根据上面的nginx.conf中指定的:

[root@Nginx conf]# mkdir -p ../html/brian
[root@Nginx conf]# mkdir -p ../html/brianzjz

    创建index.html的首页文件

[root@Nginx nginx]# echo "http://www.brian.com" > html/brian/index.html
[root@Nginx nginx]# echo "http://www.brianzjz.com" > html/brianzjz/index.html
[root@Nginx nginx]# cat html/brian/index.html
http://www.brian.com
[root@Nginx nginx]# cat html//brianzjz/index.html
http://www.brianzjz.com

   这样我们配置文件和首页就已经配置完成了,下面就是重启nginx 并且进行测试了 

  2.3 重启nginx,测试:

    在重启nginx服务之前,我们要先对语法进行检测:

[root@Nginx nginx]# sbin/nginx -t               # 语法检测,出现ok字样,没有问题
nginx: the configuration file /opt/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.6.3//conf/nginx.conf test is successful

    平滑重启nginx服务(reload方法,不中断业务)

[root@Nginx nginx]# sbin/nginx -s reload

    检查nginx的重新加载情况

[root@Nginx nginx]# netstat -lntup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23305/nginx: master
[root@Nginx nginx]# ps -ef | grep nginx
root 23305 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx 24379 23305 0 10:44 ? 00:00:00 nginx: worker process
root 24383 23911 0 10:44 pts/2 00:00:00 grep --color=auto nginx
[root@Nginx nginx]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 23305 root 6u IPv4 47849 0t0 TCP *:http (LISTEN)
nginx 24379 nginx 6u IPv4 47849 0t0 TCP *:http (LISTEN)

    测试(因为没有dns的域名解析服务,我们只能通过添加hosts的文件来模拟解析):

Linux添加hosts文件:
[root@Nginx nginx]# echo "192.168.1.108 www.brian.com www.brianzjz.com" >> /etc/hosts
[root@Nginx nginx]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.108 www.brian.com www.brianzjz.com windows添加hosts文件
路径:C:\Windows\System32\drivers\etc\hosts
打开上面路径的文件添加,下面内容 保存
192.168.1.108 www.brian.com www.brianzjz.com

    Linux上面访问测试:

[root@Nginx conf]# curl www.brian.com
www.brian.com                # www.brian.com 主页面内容
[root@Nginx conf]# curl www.brianzjz.com
www.brianzjz.com # www.brianzjz.com 主页面内容
[root@Nginx conf]#

    windows浏览器测试:

  

3、基于端口的虚拟主机(基于上面的配置文件进行修改)

  3.1 修改配置文件:

    修改nginx.conf配置文件如下:(红色标记修改位置)

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen ; # 将80端口修改成10000
server_name www.brian.com;
location / {
root html/brian;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
} }
server {
listen ; # 将80端口修改成11000
server_name www.brianzjz.com;
location / {
root html/brianzjz;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
} }
}

  3.2 重启nginx,测试:

    在重启nginx服务之前,我们要先对语法进行检测:

[root@Nginx nginx]# sbin/nginx -t               # 语法检测,出现ok字样,没有问题
nginx: the configuration file /opt/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.6.3//conf/nginx.conf test is successful

    平滑重启nginx服务(reload方法,不中断业务)

[root@Nginx nginx]# sbin/nginx -s reload

    检查nginx的重新加载情况

[root@Nginx nginx]# netstat -lntup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23305/nginx: master
[root@Nginx nginx]# ps -ef | grep nginx
root 23305 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx 24379 23305 0 10:44 ? 00:00:00 nginx: worker process
root 24383 23911 0 10:44 pts/2 00:00:00 grep --color=auto nginx
[root@Nginx nginx]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 23305 root 6u IPv4 47849 0t0 TCP *:http (LISTEN)
nginx 24379 nginx 6u IPv4 47849 0t0 TCP *:http (LISTEN)

    windows浏览器测试:

4、基于IP的虚拟主机(基于域名的虚拟主机进行配置文件修改)

  4.1 增加服务器IP:

    既然要配置基于IP的虚拟主机,就需要让每个虚拟主机的IP不同,这时我们就要加入辅助的虚拟IP了,命令如下:

[root@Nginx nginx]# ip addr add 192.168.1.111/24 dev ens33

    增加完毕后,检查配置生效结果:

[root@Nginx nginx]# ip add | grep 192.168.1    # 过滤ens33 网卡上的IP
inet 192.168.1.108/24 brd 192.168.1.255 scope global dynamic ens33
inet 192.168.1.111/24 scope global secondary ens33
[root@Nginx nginx]# ping 192.168.1.108 # ping检查
PING 192.168.1.108 (192.168.1.108) 56(84) bytes of data.
64 bytes from 192.168.1.108: icmp_seq=1 ttl=64 time=0.387 ms
[root@Nginx nginx]# ping 192.168.1.111 # ping 检查
PING 192.168.1.111 (192.168.1.111) 56(84) bytes of data.
64 bytes from 192.168.1.111: icmp_seq=1 ttl=64 time=0.050 ms

  4.2 修改配置文件:(红色标记为修改项)

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen 192.168.1.108:80;
server_name www.brian.com;
location / {
root html/brian;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
} }
server {
listen 192.168.1.111:80;
server_name www.brianzjz.com;
location / {
root html/brianzjz;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
} }
}

  4.3 重启nginx,测试:

    在重启nginx服务之前,我们要先对语法进行检测:

[root@Nginx nginx]# sbin/nginx -t               # 语法检测,出现ok字样,没有问题
nginx: the configuration file /opt/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.6.3//conf/nginx.conf test is successful

    平滑重启nginx服务(reload方法,不中断业务)

[root@Nginx nginx]# sbin/nginx -s reload

    检查nginx的重新加载情况

[root@Nginx nginx]# netstat -lntup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23305/nginx: master
[root@Nginx nginx]# ps -ef | grep nginx
root 23305 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx 24379 23305 0 10:44 ? 00:00:00 nginx: worker process
root 24383 23911 0 10:44 pts/2 00:00:00 grep --color=auto nginx
[root@Nginx nginx]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 23305 root 6u IPv4 47849 0t0 TCP *:http (LISTEN)
nginx 24379 nginx 6u IPv4 47849 0t0 TCP *:http (LISTEN)

    windows浏览器测试:

5、总结虚拟主机的配置步骤

  • 增加一个完整的server标签段到结尾处
  • 更改server_name及对应网页的root根目录(站点根目录)
  • 创建server_name域名对应的网页根目录,并建立index.html文件
  • 检查nginx配置文件语法,平滑重启nginx,快速检查启动结果
  • 配置hosts文件或者DNS域名解析
  • 测试访问结果

Nginx的虚拟主机的更多相关文章

  1. 烂泥:使用nginx利用虚拟主机搭建WordPress博客

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近开始打算学习nginx web服务器,既然是学习还是以实用为目的的.我们在此以搭建WordPress博客为例. 搭建WordPress博客,我们需要 ...

  2. nginx多虚拟主机优先级location匹配规则及tryfiles的使用

    nginx多虚拟主机优先级location匹配规则及tryfiles的使用 .相同server_name多个虚拟主机优先级访问 .location匹配优先级 .try_files使用 .nginx的a ...

  3. [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向

    Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...

  4. nginx配置虚拟主机vhost的方法详解

    Nginx vhost配置,可实现基于ip.端口号.servername的虚拟主机,同时可避免直接修改主配置文件.在nginx下配置虚拟主机vhost非常方便.这篇文章主要介绍了nginx配置虚拟主机 ...

  5. nginx的安装 、Nginx默认虚拟主机、nginx用户认证、nginx 域名重定向

    1.nginx:官网:www.nginx.org 下载:wget -c  http://nginx.org/download/nginx-1.14.0.tar.gz解压:tar -zxvf nginx ...

  6. nginx 配置虚拟主机访问PHP文件 502错误的解决方法

    最近配置Nginx 服务器虚拟主机 访问目录发现报502错误 百度了很多方法 都不管用  我擦 各种抓狂----- 原本Nginx配置如下: 网上找了很多方法: 查看日志   借助nginx的错误日志 ...

  7. Nginx中虚拟主机配置

    一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...

  8. centos7 安装 iRedmail 后 给nginx添加虚拟主机

    iRedmail安装参考官方文档和 https://ywnz.com/linuxyffq/4563.html 准备工作 更新操作系统 yum update -y 安装必要组件 yum install ...

  9. nginx配置虚拟主机、反向代理和负载均衡

    为了实现这个功能,需要修改nginx的配置文件,将nginx.conf清理一下,使结构更清晰. worker_processes ; events { worker_connections ; } h ...

  10. nginx 配置虚拟主机

    文章转载自:http://www.ttlsa.com/html/1571.html 上篇说道我们的nginx是安装在/usr/local/nginx/ cd conf 我们现在把所有的虚拟主机放在一个 ...

随机推荐

  1. SpaceSyntax【空间句法】之DepthMapX学习:第四篇 凸多边形图分析[未完]

    这一篇正式讲解分析类型中的第一个,凸多边形分析,流程图参照上一篇的. 博客园/B站/知乎/CSDN @秋意正寒(我觉得这一篇肯定很多盗图的,那么我在版头加个本篇地址吧) https://www.cnb ...

  2. Nutch的nutch-default.xml和regex-urlfilter.txt的中文解释

    nutch-default解释.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl&qu ...

  3. 一口一口吃掉Hexo(四)

    如果你想得到更好的阅读效果,请访问我的个人网站 ,版权所有,未经许可不得转载! 人总是不会满足于现状,接下来我们就可以让我们的朋友们通过独立域名访问我们的网站了,但是这肯定是要花点钱的,所以这篇文章难 ...

  4. VMware虚拟机克隆Linux(CentOS)系统后找不到eth0网卡的问题(图文详解)

     问题现象: 有时候,会使用VMware虚拟机的的克隆功能,快速的复制已安装好的Linux系统. 可是克隆完之后,会发现没有eth0网卡. 解决办法: 1.编辑/etc/udev/rules.d/70 ...

  5. java-数组连接的几种方式

    多个数组进行拼接, 1, 使用java自己的 System#arrayCopy() byte[] message = new byte[heads.length + result.length + b ...

  6. 【IT笔试面试题整理】寻找二叉树两节点的最近的公共祖先

    [试题描述] 求二叉树中任意两个节点的最近公共祖先也称为LCA问题(Lowest Common Ancestor). 二叉查找树 如果该二叉树是二叉查找树,那么求解LCA十分简单. 基本思想为:从树根 ...

  7. How to describe the wind sprial in computer system?

    How to describe the wind sprial in computer system? 2017-02-21 刘崇军 风螺旋线 If we want get the approval ...

  8. UVa 442 Matrix Chain Multiplication(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...

  9. [SPOJ 687]Repeats

    Description 题库链接 给出一个长度为 \(n\) 的字符串,求重复次数最多的连续重复子串. \(1\leq n\leq 50000\) Solution Code #include < ...

  10. GCD之定时器dispatch_source_t(转载暂时未完全理解)

    #import "ViewController.h" @interface ViewController (){ IBOutlet UIButton *l_timeButton; ...