Nginx服务--构建虚拟主机

一.构建虚拟主机概述

  • 利用虚拟主机,不用为每个运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程,虚拟主机提供了在同一台服务器、同一组Nginx进程上运行的多个网站的功能
  •     与Apache相同,Nginx也可以配置多种类型的虚拟主机,分别是基于域名、基于端口、基于IP地址的虚拟主机
  •     使用Nginx搭建虚拟主机服务器时,每个虚拟web站点拥有独立的“server {}”配置段,各自监听的IP地址、端口号段单独指定

二.基于域名的虚拟主机

  • 安装DNS服务
[root@localhost nginx]# yum install bind -y
[root@localhost nginx]# vim /etc/named.conf
12 options {
 13         listen-on port 53 { any; };    //修改为any
 14         listen-on-v6 port 53 { ::1; };
 15         directory       "/var/named";
 16         dump-file       "/var/named/data/cache_dump.db";
 17         statistics-file "/var/named/data/named_stats.txt";
 18         memstatistics-file "/var/named/data/named_mem_stats.txt";
 19         recursing-file  "/var/named/data/named.recursing";
 20         secroots-file   "/var/named/data/named.secroots";
 21         allow-query     { any; };      //修改为any

[root@localhost nginx]# vim /etc/named.rfc1912.zones
25 zone "kgc.com" IN {                   //修改域名为kgc.com
 26         type master;
 27         file "kgc.com.zone";         //修改为kgc.com.zone
 28         allow-update { none; };
 29 };
 30 
 31 zone "yun.com" IN {                  //复制模板,将域名更改为yun.com
 32         type master;
 33         file "yun.com.zone";
 34         allow-update { none; };
 35 };
[root@localhost nginx]# cd /var/named/
[root@localhost named]# cp -p named.localhost kgc.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
                                          //AAAA(IPv6)删除
www IN  A       192.168.179.180          //www解析为本机IP地址
[root@localhost named]# cp -p kgc.com.zone yun.com.zone
  • 测试DNS服务
[root@localhost named]# systemctl start named

Nginx服务--构建虚拟主机_第1张图片

  • 手工编译安装Nginx服务 
//解压缩包
tar zxvf nginx-1.12.2.tar.gz -C /opt

//创建用户
useradd -M -s /sbin/nologin nginx

//安装环境依赖包
yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y

//配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

//安装
make && make install

//创建软连接,方便命令文件管理
ln -s /usr/local/nginx/sbin/* /usr/local/sbin

便于服务管理
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in
 start)
        $PROG;;
 stop)
        kill -s QUIT $(cat $PIDF);;
 restart)
        $0 stop
        $0 start;;
 reload)
        kill -s HUP $(cat $PIDF);;
 *)
        exit 1
esac
exit 0

//添加执行权限
chmod +x /etc/init.d/nginx

//添加为系统服务
chkconfig --add nginx 


  • 基于域名构建虚拟主机
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
//在原有的server更改
server {
 36         listen       80;
 37         server_name  www.kgc.com;
 38         location / {
 39             root   /var/www/kgc;
 40             index  index.html index.htm;
 41         }
 42         error_page   500 502 503 504  /50x.html;
 43         location = /50x.html {
 44             root   html;
 45         }
 46     }
 47 //复制域名kgc.com,修改域名为yun.com
 48     server {
 49         listen       80;
 50         server_name  www.yun.com;
 51         location / {
 52             root   /var/www/yun;
 53             index  index.html index.htm;
 54         }
 55         error_page   500 502 503 504  /50x.html;
 56         location = /50x.html {
 57             root   html;
 58         }
 59     }
  • 创建主页面文件
mkdir -p /var/www/kgc
mkdir -p /var/www/yun
echo "this is kgc web"  >/var/www/kgc/index.html
echo "this is yun web"  >/var/www/yun/index.html

//重启服务访问

Nginx服务--构建虚拟主机_第2张图片

Nginx服务--构建虚拟主机_第3张图片

三.基于端口的虚拟主机

  •  基于端口的虚拟主机和基于域名配置基本一样,区别在于,Nginx的主配置文件中,修改server{}参数
[root@localhost www]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen      192.168.179.180:80;          //主机监听地址要改为本机IP地址
        server_name  www.kgc.com;
        location / {
            root   /var/www/kgc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
    server {
        listen      192.168.179.180:8080;
        server_name  www.kgc.com;
        location / {
            root   /var/www/kgc80;                
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
  • 创建站点目录kgc80,
[root@localhost www]# mkdir -p /var/www/kgc80
[root@localhost www]# echo "this is kgc8080 web" >/var/www/kgc80/index.html
  • 重启服务,重新访问
[root@localhost www]# service nginx stop
[root@localhost www]# service nginx start
[root@localhost www]# netstat -ntap | grep nginx
tcp        0      0 192.168.179.180:8080    0.0.0.0:*               LISTEN      43871/nginx: master 
tcp        0      0 192.168.179.180:80      0.0.0.0:*               LISTEN      43871/nginx: master 
[root@localhost www]#

Nginx服务--构建虚拟主机_第4张图片

 Nginx服务--构建虚拟主机_第5张图片

你可能感兴趣的:(Nginx服务--构建虚拟主机)