Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机
admin
2023-02-28 16:22:09
0

Nginx支持的虚拟主机有三种

●基于域名的虚拟主机
●基于IP的虚拟主机
●基于端口的虚拟主机
每一种虚拟主机均可通过“server{}" 配置段实现各自的功能

基于域名的虚拟主机

实验环境

1.基础源码包(无密码):https://pan.baidu.com/s/14WvcmNMC6CFX1SnjHxE7JQ
2.CentOS 7版本Linux虚拟机
主机IP 域名
192.168.235.158 www.kgc.com,www.accp.com

实验步骤

一、编译安装Nginx服务

第一步:远程获取Windows上的源码包,并挂载到Linux上

[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password: 
Sharename       Type      Comment
---------       ----      -------
LNMP            Disk  

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:  
[root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz

第二步:解压源码包

[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0  rh

第三步:下载安装编译组件包

[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \             //C语言
> gcc-c++ \         //c++语言
> pcre-devel \      //pcre语言工具
> zlib-devel        //压缩函数库

第四步:创建程序用户并配置Nginx服务相关组件

[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//创建程序用户nginx,并限定其不可登录终端
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \            
//配置nginx
> --prefix=//usr/local/nginx \      
//指定安装路径                        
> --user=nginx \
//指定用户名
> --group=nginx \
//指定用户所属组
> --with-http_stub_status_module
//安装状态统计模块

第五步:编译与安装Nginx

[root@localhost nginx-1.12.0]# make && make install

第六步:优化Nginx服务启动脚本,并建立命令软连接

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
//创建nginx服务命令软链接到系统命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service 
//关闭防火墙
[root@localhost nginx-1.12.0]# setenforce 0
//关闭增强型安全功能
[root@localhost nginx-1.12.0]# nginx 
//输入nginx 开启服务
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80      //查看服务的80 端口,显示已开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7520/nginx: master  

二、配置DNS域名解析服务

[root@localhost ~]# yum -y install bind
//安装DNS服务的bind包
[root@localhost ~]# vim /etc/named.conf 
//编辑主配置文件

options {
        listen-on port 53 { any; };
        ##将监听地址127.0.0.1替换为any,
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };
        ##将授权localhost替换为any

[root@localhost ~]# vim /etc/named.rfc1912.zones 
//编辑两个域名的区域配置文件

zone "kgc.com" IN {        
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};      

zone "accp.com" IN {        
        type master;
        file "accp.com.zone";
        allow-update { none; };
};      

[root@localhost ~]# cd /var/named
[root@localhost named]# cp -p named.localhost kgc.com.zone 
[root@localhost named]# cp -p named.localhost accp.com.zone 
[root@localhost named]# vim kgc.com.zone 
//编辑kgc域名区域数据配置文件
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.235.158
##删除原来末行的内容,添加域名解析地址为本机地址

[root@localhost named]# vim accp.com.zone 
//编辑accp域名区域数据配置文件
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.235.158
##删除原来末行的内容,添加域名解析地址为本机地址

[root@localhost named]# systemctl start named   
//开启dns服务
[root@localhost named]# systemctl stop firewalld.service    
//关闭防火墙
[root@localhost named]# setenforce 0   
//关闭增强型安全功能

三、配置虚拟主机

第一步:创建测试网页

[root@localhost named]# cd 
[root@localhost ~]# mkdir -p /var/www/html/kgc
[root@localhost ~]# mkdir -p /var/www/html/accp
[root@localhost ~]# ls /var/www/html/
accp  kgc
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this kgc web" > kgc/index.html
[root@localhost html]# echo "this accp web" > accp/index.html

第二步:编辑nginx.conf配置文件

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

server {
        listen       80;
        server_name  www.kgc.com;

        charset utf-8;
        ##支持中文字符
        access_log  logs/www.kgc.com.access.log;
        ##kgc站点访问日志
        location / {
                    root   /var/www/html/kgc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        ##服务端报错相关网页
        location = /50x.html {
            root   html;
        }
 }

server {
        listen       80;
        server_name  www.accp.com;

        charset utf-8;

        access_log  logs/www.accp.com.access.log;

        location / {
            root   /var/www/html/accp;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

第三步:重载Nginx服务

[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6117/nginx: master 

第四步:测试网页,输入www.kgc.com与www.accp.com两个域名进行访问
Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机
Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机



基于端口的虚拟主机

配置虚拟主机

第一步:创建另一个端口的测试网页

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html 

第二步:编辑nginx.conf配置文件,仅修改监听地址

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

server {
        listen 192.168.235.158:80;
        ##监听主机的80端口
        server_name  www.kgc.com;

        charset utf-8;

        access_log  logs/www.kgc.com.access.log;

        location / {
            root   /var/www/html/kgc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

server {
        listen 192.168.235.158:8080;
        ##监听主机的8080端口
        server_name  www.kgc.com;

        charset utf-8;

        access_log  logs/www.kgc.com.access.log;

        location / {
            root   /var/www/html/kgc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

第三步:重载Nginx服务

[root@localhost html]# killall -s HUP nginx
[root@localhost html]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      41958/nginx: master 

第四步:测试网页,分别访问80端口的默认网页以及8080端口的网页
Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机
Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机

基于IP的虚拟主机

第一步:添加网卡,并规划域名IP

主机IP 域名
192.168.235.158 www.kgc.com
192.168.235.142 www.accp.com

第二步: 修改accp域名的区域数据文件配置

[root@localhost ~]# vim /var/named/accp.com.zone

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.235.142
##更改IP地址为 192.168.235.142

[root@localhost ~]# systemctl restart named
##重启域名解析服务

第三步:编辑nginx.conf配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
##此段不做修改
server {
        listen 192.168.235.158:80;
        server_name  www.kgc.com;

        charset utf-8;

        access_log  logs/www.kgc.com.access.log;

        location / {
            root   /var/www/html/kgc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

server {
         listen 192.168.235.142:80;
         ##修改本段监听地址为192.168.234.142
         server_name  www.accp.com;

         charset utf-8;

         access_log  logs/www.accp.com.access.log;

         location / {
             root   /var/www/html/accp;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
  }

第四步:重载Nginx服务

[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# netstat -ntap | grep 80
tcp        0      0 192.168.235.142:80      0.0.0.0:*               LISTEN      7299/nginx: master  
tcp        0      0 192.168.235.158:80      0.0.0.0:*               LISTEN      7299/nginx: master  

第五步:测试网页,分别输入IP地址192.168.235.158和192.168.235.142进行访问

Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机
Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机

到此便是Nginx虚拟主机应用的全部内容了,谢谢阅读!!!

相关内容

热门资讯

国家防总提升针对广东的应急响应... 今年第12号台风“红霞”已于7月25日早晨加强为台风级。预计,“红霞”将于25日夜间至26日早晨在香...
美媒爆猛料:两国首次直接袭击伊... 多名知情人士向美国《华尔街日报》透露,巴林和科威特在7月初对伊朗境内军事目标实施了秘密空袭,知情人士...
郑丽文:只有中国国民党可以确保... 据凤凰卫视报道,国民党今天(7月25日)举行“全代会”,党主席郑丽文与党内提名的县市长参选人一同造势...
透视上半年消费外贸外资走势 新华社北京7月23日电 题:稳中提质 韧性增强——透视上半年消费外贸外资走势新华社记者谢希瑶、王聿昊...
水龙头出水小怎么解决 水龙头出水小可能是由多种原因引起的,以下是一些常见的解决方法:1. 清洁水龙头:水龙头使用时间长了,...
小神童洗衣机启动后不出水直接洗 有可能是洗衣机的进水阀被烧坏了,需要换一个新的进水阀安装上即可。另外不建议自己擅自拆卸洗衣机内部进行...
祝贺景德镇! 海报制作:苗夏阳正在韩国釜山举行的联合国教科文组织第48届世界遗产大会25日通过决议将“景德镇手工瓷...
中央空调不制冷怎么办-中央空调... 中央空调,是电器股份有限公司主要产品之一,但是由于中央空调使用环境和使用方式的不同,在使用过程中就会...
空调不出水不制冷怎么回事 内罩长期不清洗,当然房间密封性差的话也会导致这种现象;其次就是外机问题,如果外机积满灰尘,会成空调制...
空调不出水,不制冷 原因有多种,可能是电源插头没有接牢、制冷剂不足、空调短时间休息间隙、空调滤网积灰等等。很多情况可以自...