标签:Linux 域名 Nginx
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://xpleaf.blog.51cto.com/9315560/1901284

0.说明

使用Nginx可以配置基于域名的虚拟主机、基于端口的虚拟主机和基于端口的虚拟主机,比较常用的是基于域名的虚拟主机,这里要做的配置是基于域名的虚拟主机,并且是配置多个基于域名的虚拟主机。

关于Nginx配置文件的说明可以参考官方文档,同时也可以参考老男孩老师的书籍《跟老男孩学Linux运维:Web集群实战》,讲解得非常好!


1.实验环境

关于Nginx的详细安装配置,可以参考另一篇博文《在CentOS上编译安装Nginx+实验环境搭建+测试》

本次实验的测试环境使用的宿主机操作系统为Windows 7,在Vmware虚拟机安装CentOS 6.5,说明如下:

  • 宿主机操作系统Windows 7

  • 虚拟机安装的操作系统CentOS 6.5

  • 虚拟机操作系统上网方式NAT

而当使用NAT的方式进行上网时虚拟机、宿主机之间的网络连接关系可如下所示:

关于为什么网络拓扑结构是这样的,这里不展开说明,可以参考博主的另一篇博文《在实践中深入理解VMware虚拟机的上网模式NAT模式》,这篇文章深入地分析了VMware虚拟机使用NAT模式上网时的网络结构细节,相信看完这篇文章后,这里搭建Nginx的实验环境也就很容易理解了。

另外需要注意的是这里安装的CentOS 6.5操作系统使用了最小化安装,并且只定制安装了一些常用的开发工具如gcc等,其版本信息如下:

1
2
3
4
5
6
[root@leaf ~]# cat /etc/redhat-release 
CentOS release 6.5 (Final)
[root@leaf ~]# uname -r
2.6.32-431.el6.x86_64
[root@leaf ~]# uname -m
x86_64

2.配置一个基于域名的虚拟主机与测试

先启动Nginx,验证服务是否正常:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@leaf ~]# /application/nginx/sbin/nginx 
[root@leaf ~]# netstat -lnp | grep 80        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6881/nginx          
unix  2      [ ACC ]     STREAM     LISTENING     9180   1/init              @/com/ubuntu/upstart
[root@leaf ~]# curl localhost
<h1>Hello, I'm xpleaf.</h1>
[root@leaf ~]# LANG=en
[root@leaf ~]# wget localhost
--2017-02-24 13:33:43--  http://localhost/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:80... failed: Connection refused.
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28 [text/html]
Saving to: `index.html.1'
 
100%[======================================>] 28          --.-K/s   in 0s      
 
2017-02-24 13:33:43 (1.87 MB/s) - `index.html.1' saved [28/28]

从上面的输出可以看到,此时Nginx是可以正常运行和提供服务的。

(1)实验准备:最小化Nginx的主配置文件nginx.conf

Nginx的配置文件在安装目录下的conf目录中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@leaf ~]# tree /application/nginx
/application/nginx
|-- client_body_temp
|-- conf
|   |-- fastcgi.conf
|   |-- fastcgi.conf.default
|   |-- fastcgi_params
|   |-- fastcgi_params.default
|   |-- koi-utf
|   |-- koi-win
|   |-- mime.types
|   |-- mime.types.default
|   |-- nginx.conf
|   |-- nginx.conf.default
|   |-- scgi_params
|   |-- scgi_params.default
|   |-- uwsgi_params
|   |-- uwsgi_params.default
|   `-- win-utf
|-- fastcgi_temp
|-- html
|   |-- 50x.html
|   |-- index.html
|   `-- index.html.source
|-- logs
|   |-- access.log
|   |-- error.log
|   `-- nginx.pid
|-- proxy_temp
|-- sbin
|   `-- nginx
|-- scgi_temp
`-- uwsgi_temp

nginx.conf便是主配置文件,nginx.conf.default则是它的备份,该配置文件有数百行:

1
2
[root@leaf conf]# wc -l nginx.conf
117 nginx.conf

为了学习的方便,可以考虑将其注释内容去掉:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@leaf conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
[root@leaf 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;
        }
    }
}
[root@leaf conf]# wc -l nginx.conf
22 nginx.conf

去掉了注释和空白行后只有22行,就很方便我们待会做实验时进行配置了。

(2)修改配置文件

假设我们的Nginx为站点www.xpleaf.cn服务,则可以将主配置文件修改为如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@leaf 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  www.xpleaf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

主要是修改了第12行和第14行,其中第14行说明该站点的根目录的html文件在html/www/目录中。

(3)创建域名对应的站点目录及文件

1
2
3
4
5
[root@leaf nginx]# cd html/
[root@leaf html]# mkdir www
[root@leaf html]# echo "This page is: www.xpleaf.cn">www/index.html 
[root@leaf html]# cat www/index.html 
This page is: www.xpleaf.cn

(4)重新启动Nginx服务

1
2
3
4
[root@leaf html]# /application/nginx/sbin/nginx -t   # 检查Nginx配置语法
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@leaf html]# /application/nginx/sbin/nginx -s reload   # 优雅重启Nginx

(5)在CentOS 6.5上进行测试

因为上面我们设置的域名www.xpleaf.cn实际是可能不存在,但为了达到测试的目的,即当访问www.xpleaf.cn时,能够解析到我们CentOS上的IP地址,从而可以访问其上面的Nginx服务,达到访问Nginx虚拟主机的目的,所以在CentOS上进行测试时,我们需要修改/etc/hosts文件,让www.xpleaf.cn解析为CentOS的IP地址:

1
2
3
[root@leaf html]# echo "127.0.0.1 www.xpleaf.cn" >>/etc/hosts
[root@leaf html]# tail -1 /etc/hosts
127.0.0.1 www.xpleaf.cn

此时,在CentOS上使用curl命令和wget命令来访问www.xpleaf.cn,查看测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@leaf html]# curl www.xpleaf.cn
This page is: www.xpleaf.cn
[root@leaf html]# wget www.xpleaf.cn
--2017-02-24 13:58:29--  http://www.xpleaf.cn/
Resolving www.xpleaf.cn... 127.0.0.1
Connecting to www.xpleaf.cn|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28 [text/html]
Saving to: `index.html.1'
 
100%[======================================>] 28          --.-K/s   in 0s      
 
2017-02-24 13:58:29 (2.24 MB/s) - `index.html.1' saved [28/28]

从输出结果可以知道,此时Nginx成功地为域名为www.xpleaf.cn的虚拟主机提供了服务。

(6)在Windows 7主机上进行测试

为了达到前面说的目的,在Windows操作系统上同样需要修改hosts文件,Windows 7的hosts文件在C:\Windows\System32\drivers\etc,同样添加下面一行:

1
10.0.0.101 www.xpleaf.cn

这时在浏览器中输入地址www.xpleaf.cn,查看返回的结果:

可以看到,可以正常访问。


3.配置多个基于域名的虚拟主机与测试

上面的实验中只有一个站点www.xpleaf.cn,假如还有两个站点bbs.xpleaf.cn和blog.xpleaf.cn,同样需要Nginx来提供服务,这时就需要配置多个基于域名的虚拟主机了,不过有了上面的基础后,下面的操作就会容易很多,因为思路都是一样的。

(1)修改主配置文件nginx.conf

在前面的基础上,修改为如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@leaf 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  www.xpleaf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  bbs.xpleaf.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  blog.xpleaf.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

(2)创建域名对应的站点目录及文件

1
2
3
4
5
6
7
[root@leaf html]# mkdir bbs
[root@leaf html]# echo "This page is: bbs.xpleaf.cn" >bbs/index.html
[root@leaf html]# mkdir blog
[root@leaf html]# echo "This page is: blog.xpleaf.cn" >blog/index.html   
[root@leaf html]# cat bbs/index.html blog/index.html 
This page is: bbs.xpleaf.cn
This page is: blog.xpleaf.cn

(3)重新启动Nginx服务

1
2
3
4
[root@leaf html]# /application/nginx/sbin/nginx -t   # 检查Nginx配置语法
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@leaf html]# /application/nginx/sbin/nginx -s reload   # 优雅重启Nginx

(4)在CentOS 6.5上进行测试

在原来基础上,修改/etc/hosts文件,在127.0.0.1地址后添加bbs.xpleaf.cn和blog.xpleaf.cn两个域名:

1
2
[root@leaf html]# tail -1 /etc/hosts
127.0.0.1 www.xpleaf.cn bbs.xpleaf.cn blog.xpleaf.cn

使用curl命令和wget命令进行测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@leaf html]# curl bbs.xpleaf.cn
This page is: www.xpleaf.cn
[root@leaf html]# curl blog.xpleaf.cn
This page is: www.xpleaf.cn
[root@leaf html]# wget bbs.xpleaf.cn
--2017-02-24 14:19:54--  http://bbs.xpleaf.cn/
Resolving bbs.xpleaf.cn... 127.0.0.1
Connecting to bbs.xpleaf.cn|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28 [text/html]
Saving to: `index.html.2'
 
100%[======================================>] 28          --.-K/s   in 0s      
 
2017-02-24 14:19:54 (2.37 MB/s) - `index.html.2' saved [28/28]
 
[root@leaf html]# wget blog.xpleaf.cn
--2017-02-24 14:20:00--  http://blog.xpleaf.cn/
Resolving blog.xpleaf.cn... 127.0.0.1
Connecting to blog.xpleaf.cn|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28 [text/html]
Saving to: `index.html.3'
 
100%[======================================>] 28          --.-K/s   in 0s      
 
2017-02-24 14:20:00 (2.24 MB/s) - `index.html.3' saved [28/28]

从上面结果可以知道,Nginx为各个虚拟主机正常提供服务。

(5)在Windows 7主机上进行测试

在原来基础上,修改hosts文件,如下:

1
10.0.0.101 www.xpleaf.cn bbs.xpleaf.cn blog.xpleaf.cn

在浏览器上分别访问各个域名,查看其返回结果:

  • 访问www.xpleaf.cn:

  • 访问bbs.xpleaf.cn:

  • 访问blog.xpleaf.cn:

可以看到访问每个域名都返回了期待的页面,说明测试成功!


6.进阶:Nginx虚拟主机的别名配置

所以虚拟主机别名,就是为虚拟主机设置除了主域名以外的一个或多个域名名字,这样就能实现用户访问的多个域名对应同一个虚拟主机网站的功能。

以www.xpleaf.cn为例,希望添加一个别名xpleaf.cn,这样当访问xpleaf.cn时,和访问www.xpleaf.cn得到的结果是一样的。

其实配置的思路非常简单,只需要在上面nginx.conf配置文件中www.xpleaf.cn的server域中再添加一个xpleaf.cn的域名就可以了,如下:

1
2
3
4
5
6
7
8
server {
        listen       80;
        server_name  www.xpleaf.com xpleaf.cn;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }

测试的话依然按照前面的方法进行,即先检查Nginx配置文件、平滑重启Nginx服务、配置hosts文件,最后通过命令行或浏览器的方式进行验证,因为跟前面是一样的,所以这里就不展开了。


5.下一步要做什么

可以考虑配置与测试基于端口的虚拟主机和基于IP地址的虚拟主机,其实只要把上面的弄清楚了,再做这些配置就会容易很多了。


7.参考资料

《跟老男孩学Linux运维:Web集群实战》

Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试的更多相关文章

  1. nginx配置基于域名的虚拟主机

    其实基于域名和基于ip的虚拟主机配置是差不多的,在配置基于ip的虚拟主机上我们只需要修改几个地方就能变成基于域名的虚拟主机,一个是要修改域名,一个是host文件直接看代码 [root@localhos ...

  2. nginx基于域名的虚拟主机配置(本地分布式项目域名配置及测试方法)

    最有用的虚拟主机配置方式. 一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定. 可以修改host文件实现域名访问. 前提:即使我们在nginx中配置基于域名的虚拟主机,也需要域名解析,即n ...

  3. Nginx总结(四)基于域名的虚拟主机配置

    前面讲了如何安装配置Nginx,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要说的 ...

  4. 十八.搭建Nginx服务器、配置网页认证、基于域名的虚拟主机、ssl虚拟主机

    配置要求: client:192.168.4.10 proxy:192.168.4.5(eth0) 192.168.2.5(eth1) web1:192.168.2.100 web2:192.168. ...

  5. Nginx--服务部署、基于域名的虚拟主机配置

    一.服务部署 1.预处理 安装CentOS ,配置hosts.静态IP.设置必要的安全参数等(略) 1-1.系统环境 [root@vnx ~]# cat /etc/redhat-release Cen ...

  6. Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)

    1.安装nginx # pcre中文"perl兼容正则表达式",安装pcre库是为了让nginx支持具备URL重写功能 # 的Rewrite模块,rewrite可以实现动态页面转成 ...

  7. 高级运维(二):搭建Nginx服务器、用户认证、基于域名的虚拟主机、SSL虚拟主机、Nginx反向代理

    一.搭建Nginx服务器 目标: 在IP地址为192.168.4.5的主机上安装部署Nginx服务,并可以将Nginx服务器,要求编译时启用如下功能: 1> SSL加密功能 2> 设置Ng ...

  8. CentOS 7运维管理笔记(8)----Apache基于域名的虚拟主机配置

    使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问. (1) 在网卡 eth0的第五个接口上配置 192.168.1.215 这个地址: (2) 配置/e ...

  9. Nginx基于域名的虚拟主机

    一.基于域名的虚拟主机 修改配置文件/usr/local/nginx/conf/nginx.conf 创建新的虚拟主机的根目录和默认网页index.html 重新加载nginx的配置文件 查看两个虚拟 ...

随机推荐

  1. 关于boost中enable_shared_from_this类的原理分析

    首先要说明的一个问题是:如何安全地将this指针返回给调用者.一般来说,我们不能直接将this指针返回.想象这样的情况,该函数将this指针返回到外部某个变量保存,然后这个对象自身已经析构了,但外部变 ...

  2. Alpha冲刺(8/10)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之8 团队部分 后敬甲(组长) 过去两天完成了哪些任务 首页重新设计 课程时间线确定 答辩准备 接下来的计划 ...

  3. ffmpeg-3.2.4-static-win32-for-XP-bin.tar.xz

    ffmpeg-3.2.4-static-win32-for-XP-bin.tar.xz ffmpeg-3.2.4-static-win32-for-XP-bin-v3.tar.xz v3版本升级了库文 ...

  4. 函数-->指定函数--->默认函数--->动态函数--> 动态参数实现字符串格式化-->lambda表达式,简单函数的表示

    #一个函数何以接受多个参数#无参数#show(): ---> 执行:show() #传入一个参数 def show(arg): print(arg) #执行 show(123) #传入两个参数 ...

  5. $Django 路由层(有,无名分组、反向解析、总路由分发、名称空间、伪静态)

    1 简单配置 -第一个参数是正则表达式(如果要精准匹配:'^publish/$')  -第二个参数是视图函数(不要加括号)  -url(r'^admin/', admin.site.urls), 注: ...

  6. java压缩图片质量

    使用了工具thumbnailator,据说thumbnailator是一个非常好的图片开源工具,使用起来很方便.不过没仔细看过,我只是需要压缩图片,让其占用空间变小而已.使用maven引入jar包 & ...

  7. fpm...failed: Cannot assign requested address

    2017年6月23日 16:00:28 星期五 启动fpm的时候总是报错, 看了网上说的修改rlimit值但是不管用 后来发现fpm配置文件监听的地址写错了, 127.0.0.1 写成了 172.0. ...

  8. nginx https配置记录

    一.证书生成: 要有两个文件,一个私钥,一个证书. 私钥:-----BEGIN PRIVATE KEY----- 开始 证书:-----BEGIN CERTIFICATE----- 开始 二.Ngin ...

  9. awk-for循环简单用法

    文本: [root@VM_0_84_centos ~]# cat sshd.txt 1 2 3 4 5 6 7 8 9 循环打印上述文本 for 循环的固定格式   i=1设置i的初始变量  i< ...

  10. 无锁编程 - Double-checked Locking

    Double-checked Locking,严格意义上来讲不属于无锁范畴,无论什么时候当临界区中的代码仅仅需要加锁一次,同时当其获取锁的时候必须是线程安全的,此时就可以利用 Double-check ...