zoukankan      html  css  js  c++  java
  • 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与基于端口的虚拟主机配置章节

  • 相关阅读:
    Python 06--面向对象编程
    Python 05--常用模块学习
    6大排序算法,c#实现
    Git管理unity3d项目
    cordova crosswalk android 7.0 问题
    ionic/cordvoa 修改platform文件夹里的文件,build会覆盖问题
    webStorm Linux Ubuntu 中文搜狗输入问题
    Ionic android 底部tabs
    ionic 添加新module
    yii2 Nav::widget() 和 Menu::widget()
  • 原文地址:https://www.cnblogs.com/guarderming/p/9466298.html
Copyright © 2011-2022 走看看