Nginx的多站点与Tomcat类似,都是新建多个目录文件作为web访问目录,然后在配置文件中关联对应的物理路径。这里我们也来看看Nginx是如何配置多站点和域名的。

基于域名的虚拟主机配置方法(最常用)

  1. 在本地电脑解析域名。

     
  2. 为Nginx创建3个web目录:www、bbs、blog,并创建对应的index.html
    [root@NFS /]# mkdir -p /application/nginx/html/{www,bbs,blog}
    [root@NFS /]# ll /application/nginx/html/
    total 12
    drwxr-xr-x 2 root root 4096 Nov 30 15:22 bbs
    drwxr-xr-x 2 root root 4096 Nov 30 15:22 blog
    drwxr-xr-x 2 root root 4096 Nov 29 19:44 www
    [root@NFS /]#
    [root@NFS /]# for name in www bbs blog;do echo "10.0.0.31 name.daydayup.com">/application/nginx/html/$name/index.html;done
    [root@NFS /]#
    [root@NFS /]# ls /application/nginx/html/www/
    index.html
    [root@NFS /]# ls /application/nginx/html/blog/
    index.html
    [root@NFS /]# ls /application/nginx/html/bbs/
    index.html
    

     

  3. 修改nginx配置文件

    [root@NFS /]# vim /application/nginx/conf/nginx.conf
    
         default_type  application/octet-stream;
         sendfile        on;
         keepalive_timeout  65;
         server {
             listen       80;
             server_name  www.daydayup.com;          #www.daydayup.com
             location / {
                 root   html/www;                    #www目录
                 index  index.html index.htm;
             }
         }
         server {
             listen       80;
             server_name  bbs.daydayup.com;          #bbs.daydayup.com
             location / {
                 root   html/bbs;                    #bbs目录
                 index  index.html index.htm;
             }
         }
         server {
             listen       80;
             server_name  blog.daydayup.com;          #blog.daydayup.com
             location / {
                 root   html/blog;                    #blog目录
                 index  index.html index.htm;
             }
         }
    
    [root@NFS /]
    [root@NFS /]# /application/nginx/sbin/nginx -t
    nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful

     

  4. 启动Nginx服务

    [root@NFS /]# /application/nginx/sbin/nginx 
    [root@NFS /]# ps -ef|grep nginx
    root       1522      1  0 15:34 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
    www        1523   1522  0 15:34 ?        00:00:00 nginx: worker process        
    root       1528   1451  0 15:35 pts/0    00:00:00 grep --color=auto nginx

     

  5. 在本地电脑访问
    www.daydayup.com


    bbs.daydayup.com


    blog.daydayup.com


     

  6. CentOS上采用curl命令通过内网地址测试。
    1.在虚拟机中添加域名解析

    [root@NFS /]# vim /etc/hosts
    
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    172.16.1.31     NFS www.daydayup.com bbs.daydayup.com blog.daydayup.com


    2.使用curl命令访问域名

    [root@NFS /]# curl www.daydayup.com
    10.0.0.31 www.daydayup.com
    [root@NFS /]# curl bbs.daydayup.com
    10.0.0.31 bbs.daydayup.com
    [root@NFS /]# curl blog.daydayup.com
    10.0.0.31 blog.daydayup.com

     

 

以上的配置方式的确没有什么问题,但是如果需要配置的站点过多,最终会导致 nginx.conf 文件越来越大,配置起来也更加麻烦了。所以我们可以为每一个站点新建一个配置文件,如下操作:

  • 查看 nginx.conf 行号
    [root@NFS ~]# cat -n /application/nginx/conf/nginx.conf
         1	worker_processes  1;
         2	events {
         3	    worker_connections  1024;
         4	}
         5	http {
         6	    include       mime.types;
         7	    default_type  application/octet-stream;
         8	    sendfile        on;
         9	    keepalive_timeout  65;
        10	    
        11	    server {
        12	        listen       80;
        13	        server_name  www.daydayup.com;
        14	        location / {
        15	            root   html/www;
        16	            index  index.html index.htm;
        17	        }
        18	    }
        19	    server {
        20	        listen       80;
        21	        server_name  bbs.daydayup.com;
        22	        location / {
        23	            root   html/bbs;
        24	            index  index.html index.htm;
        25	        }
        26	    }
        27	    server {
        28	        listen       80;
        29	        server_name  blog.daydayup.com;
        30	        location / {
        31	            root   html/blog;
        32	            index  index.html index.htm;
        33	        }
        34	    }
        35	}
    [root@NFS ~]# 
  • 创建文件夹 extra 并为每个站点创建配置文件
    [root@NFS /]# cd /application/nginx/conf/
    [root@NFS conf]# mkdir extra
    [root@NFS conf]#
    [root@NFS conf]# sed -n '11,18p' nginx.conf > extra/www.conf
    [root@NFS conf]# sed -n '19,26p' nginx.conf > extra/bbs.conf
    [root@NFS conf]# sed -n '27,34p' nginx.conf > extra/blog.conf
  • 修改主配置文件 nginx.conf ,删掉每个站点对应的server区块,采用include方式引入配置文件。
    [root@NFS conf]# vim nginx.conf
    
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    
        include     extra/www.conf;        #将www.conf文件引入
        include     extra/bbs.conf;        #将bbs.conf文件引入
        include     extra/blog.conf;       #将blog.conf文件引入
    }
  • 检查语法,并重启Nginx服务
    [root@NFS conf]# cd ..
    [root@NFS nginx]# sbin/nginx -t     #检查语法
    nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful
    [root@NFS nginx]#
    [root@NFS nginx]# sbin/nginx -s reload    #重启nginx服务
    [root@NFS nginx]#
    [root@NFS nginx]# curl www.daydayup.com    #测试
    10.0.0.31 www.daydayup.com
    [root@NFS nginx]# curl bbs.daydayup.com    #测试
    10.0.0.31 bbs.daydayup.com
    [root@NFS nginx]# curl blog.daydayup.com    #测试
    10.0.0.31 blog.daydayup.com
    [root@NFS nginx]# 

 

基于端口的虚拟主机配置方法

  • 修改 bbs.conf 文件的端口为8080。
    [root@NFS ~]# cd /application/nginx/conf/
    [root@NFS conf]# vim extra/bbs.conf 
    
        server {
            listen       8080;
            server_name  bbs.daydayup.com;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    
  • 检查语法,重启nginx服务
    [root@NFS conf]# /application/nginx/sbin/nginx -t
    nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful
    [root@NFS conf]# /application/nginx/sbin/nginx -s reload
  • 本地电脑浏览器访问 bbs.daydayup.com:8080。

     
  • 如果直接访问 bbs.daydayup.com (不加8080)。

    直接访问 bbs.daydayup.com (不加8080),那么直接访问的是www.daydayup.com的内容,这是为什么呢?
    说明:当你访问的网站域名在虚拟主机配置中不存在时,默认会将第一个虚拟主机的配置页面响应给用户
     

 

 

基于IP地址的虚拟主机配置方法

  • 修改 bbs.conf 文件如下:
    [root@NFS conf]# vim extra/bbs.conf 
    
        server {
            listen       10.0.0.31:80;
            server_name  bbs.daydayup.com;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    
  • 修改主配置文件,注释掉其他两个配置。
    [root@NFS conf]# vim nginx.conf
    
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    
        #include    extra/www.conf;
        include     extra/bbs.conf;
        #include    extra/blog.conf;
    }
    
  • 重启Nginx服务(说明:nginx服务中只要涉及IP地址的修改,都需要重启nginx服务,而不能采用平滑重启
    [root@NFS conf]# /application/nginx/sbin/nginx -s stop
    [root@NFS conf]# /application/nginx/sbin/nginx 
  • 浏览器通过IP(10.0.0.31) 访问

 

 

最后修改于 2019-12-03 10:00:13
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇