微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LNMP架构应用实战——Nginx配置虚拟主机

               


LNMP架构应用实战——Nginx配置虚拟主机


       前面介绍了Nginx服务的安装与配置文件,今天介绍下它的另一种实用配置——“虚拟主机”,每个虚拟主机可以是一个独立的网站,可以具有独立的域名,同一台服务器上的不同的虚拟主机之间是独立的,用户访问不同虚拟主机如同访问不同的服务器一样,因此它不需要为一个单独的WEB站点提供单独一个Nginx服务器和一个单独的Nginx进程


1、Nginx虚拟主机简单介绍

同apache服务一样,它也有三种不同的虚拟主机,基于域名的虚拟主机、基于IP的虚拟主机与基于端口的虚拟主机,至于其中的区别,请参考前面的 apache服务器的虚拟主机章节的相关介绍




2、Nginx虚拟主机配置环境

系统环境

[root@centos6 scripts]# cat /etc/redhat-release 

CentOS release 6.5 (Final)

[root@centos6 scripts]# uname -r

2.6.32-431.el6.x86_64

Nginx服务的版本

[root@centos6 scripts]# /application/Nginx/sbin/Nginx -v

Nginx version: Nginx/1.10.1




3、Nginx虚拟主机配置准备

生产环境的规范很重要,这是运维的重点,因此,配置前创建站点目录

[root@centos6 ~]# mkdir /www/{www,bbs,blog} -p

[root@centos6 ~]# tree /www

/www

+-- bbs

+-- blog

+-- www

3 directories, 0 files

用于存放站点文件的目录

---------------

授权目录

--------------

[root@centos6 ~]# chown -R Nginx.Nginx /www

[root@centos6 ~]# ll /www/

total 12

drwxr-xr-x. 2 Nginx Nginx 4096 Dec  4 18:38 bbs

drwxr-xr-x. 2 Nginx Nginx 4096 Dec  4 18:38 blog

drwxr-xr-x. 2 Nginx Nginx 4096 Dec  4 18:38 www

------------------------------------------------------

接下来写点小编进去吧,用于后面的测试

-----------------------------------------------------

[root@centos6 ~]# echo "welcont to mingongge's web stie" >/www/www/index.html

[root@centos6 ~]# echo "welcont to mingongge's bbs stie" >/www/bbs/index.html   

[root@centos6 ~]# echo "welcont to mingongge's blog stie" >/www/blog/index.html  

[root@centos6 ~]# cat /www/www/index.html 

welcont to mingongge's web stie

[root@centos6 ~]# cat /www/bbs/index.html 

welcont to mingongge's bbs stie

[root@centos6 ~]# cat /www/blog/index.html 

welcont to mingongge's blog stie

     生产环境检查也同样很重要,检查、检查 、检查,重要的事情说三遍!!!!




4、Nginx虚拟主机配置

      配置Nginx 虚拟主机有两种方式,一种可以像前面apache服务这种,单独配置一个虚拟主机的配置文件,另一种也可以在主配置文件 Nginx.conf中添加server标签,接下来介绍的是第一种方式,通过在配置文件添加包含关系,单独配置虚拟主机的配置文件目录与配置文件,这样实际生产环境中比较常见,服务多了,也容易维护。

--------------------

配置主配置文件

-------------------

只需要在主配置文件Nginx.conf最后一行加下如下配置

     include  extra/vhosts/*.conf;

-----------------------------

创建虚拟主机配置目录

----------------------------

[root@centos6 conf]# pwd

/application/Nginx/conf

[root@centos6 conf]# mkdir -p extra/vhosts

-----------------------------

创建虚拟主机配置文件

----------------------------

[root@centos6 conf]# cd extra/vhosts/

[root@centos6 vhosts]# mkdir bbs www blog

[root@centos6 vhosts]# cp ../../Nginx.conf www/www.conf

[root@centos6 vhosts]# cp ../../Nginx.conf bbs/bbs.conf

[root@centos6 vhosts]# cp ../../Nginx.conf blog/blog.conf


通过主配置文件修改,其实只需要里面的server标签内容,同样也可以做一个模板文件出来,通过cp命令改名后,再修改内容效果都一样


-----------------------------

配置虚拟主机配置文件

----------------------------


WWW站点虚拟主机配置文件(比较简单)

server {

        listen       80;

        server_name  www.mingongge.com;

        location / {

            root   /www/www;

            index  index.html index.htm;

        }

        }           

基它的相同,只需要改下站点目录路径与域名信息


BBS站点虚拟主机配置文件

server {

        listen       80;

        server_name  bbs.mingongge.com;

        location / {

            root   /www/bbs;

            index  index.html index.htm;

        }

        } 


BLOG站点虚拟主机配置文件

server {

        listen       80;

        server_name  blog.mingongge.com;

        location / {

            root   /www/blog;

            index  index.html index.htm;

        }

        } 


5、重启服务与测试访问

[root@centos6 vhosts]# /application/Nginx/sbin/Nginx -t                 

Nginx: the configuration file /application/Nginx-1.10.1/conf/Nginx.conf Syntax is ok

Nginx: configuration file /application/Nginx-1.10.1/conf/Nginx.conf test is successful

[root@centos6 vhosts]# /application/Nginx/sbin/Nginx -s reload

[root@centos6 vhosts]# lsof -i :80

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

Nginx   2469  root    6u  IPv4  15462      0t0  TCP *:http (LISTEN)

Nginx   2519 Nginx    6u  IPv4  15462      0t0  TCP *:http (LISTEN)

[root@centos6 vhosts]# ps -ef|grep Nginx

root 2469 1 0 18:47 ? 00:00:00 Nginx: master process /application/Nginx/sbin/Nginx

Nginx 2519 2469  0 19:14 ?00:00:00 Nginx: worker process  


打开浏览器测试访问吧

本地DNS解析不要忘记了,否则无法通过域名来访问的


图片

图片


图片

至此Nginx 虚拟主机配置完成,基于两种方式的虚拟主机配置,请参考apache服务的基于IP与基于端口的虚拟主机配置章节



版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐