资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

LNMP架构中Nginx如何配置虚拟主机

这篇文章给大家分享的是有关LNMP架构中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解析不要忘记了,否则无法通过域名来访问的


LNMP架构中Nginx如何配置虚拟主机

LNMP架构中Nginx如何配置虚拟主机

LNMP架构中Nginx如何配置虚拟主机

感谢各位的阅读!关于“LNMP架构中Nginx如何配置虚拟主机”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


文章标题:LNMP架构中Nginx如何配置虚拟主机
网页路径:http://cdkjz.cn/article/piejis.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220