Nginx虚拟主机之域名,端口,IP
admin
2023-03-29 08:41:14
0

Nginx虚拟主机之域名,端口,IP

要nginx源码包请私信我

Nginx虚拟主机之域名

[root@localhost ~]# yum install bind -y

配置DNS主配置文件

[root@localhost ~]# vim /etc/named.conf 

options {
        listen-on port 53 { 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; };

配置区域配置文件

[root@localhost ~]# vim /etc/named.rfc1912.zones 

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

zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.

配置区域数据配置文件

[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost cwq.com.zone
[root@localhost named]# vim cwq.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.136.162

[root@localhost named]# cp -p cwq.com.zone kgc.com.zone 

开启DNS服务

[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 0

去win10去测试一下能不能解析域名

Nginx虚拟主机之域名,端口,IP

安装nginx环境包

[root@localhost named]# yum install gcc gcc-c++ pcre-devel zlib-devel -y

[root@localhost named]# mkdir /abc
[root@localhost named]# mount.cifs //192.168.100.23/LNMP /abc
Password for root@//192.168.100.23/LNMP:  
[root@localhost named]# cd /abc/
[root@localhost abc]# ls
mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
nginx-1.12.0.tar.gz        php-7.1.10.tar.bz2
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/

编译安装

[root@localhost abc]# cd /opt/
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx  #创建程序性用户去管理,-M没有家目录,-s不登录本地控制台
./configure \
--prefix=/usr/local/nginx \  #指定路径
--user=nginx \  #指定用户
--group=nginx \  #指定组
--with-http_stub_status_module #状态统计模块

[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install

在它编译的时候我们再开一台终端去创建站点网页

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

做软链接,测试语法

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

写nginx脚本放在系统启动脚本中方便service管理器管理

[root@localhost nginx-1.12.2]# cd /etc/init.d/

[root@localhost init.d]# vim nginx  

#!/bin/bash
#chkconfig: - 99 20  #注释信息
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"  #这个变量,指向我的命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"  #这个变量,指向nginx的进程号
case "$1" in
    start)
        $PROG                                              
        ;;
    stop)
        kill -s QUIT $(cat $PIDF) 
        ;;
    restart)                                                  
        $0 stop
        $0 start
        ;;
    reload)                                                  
        kill -s HUP $(cat $PIDF)
        ;;
    *)                                                           
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0

[root@localhost init.d]# chmod +x nginx  #添加执行权限
[root@localhost init.d]# chkconfig --add nginx #添加nginx
[root@localhost init.d]# service nginx start 
[root@localhost init.d]# netstat -ntap | grep nginx  #查看nginx端口有没有被提供出来
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LIST
EN      42982/nginx: master

再用win10测试一下,我们需要的是不同的域名访问不同的网页

Nginx虚拟主机之域名,端口,IPNginx虚拟主机之域名,端口,IP

编辑nginx配置文件

[root@localhost init.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

#sever区域中其他多余的全删掉
server {
         listen       80;
 33         server_name  www.cwq.com;  #域名
 34         charset utf-8;  #识别中文字符集
 35         access_log  logs/www.cwq.com.access.log; #域名日志文件
 36         location / {
 37             root   /var/www/html/cwq;  #指定站点
 38             index  index.html index.htm;
 39         }   
 40         error_page   500 502 503 504  /50x.html;
 41         location = /50x.html {
 42             root   html;
 43         }   
 44     }   
 45     
 46     
47     server {
 48         listen       80;
 49         server_name  www.kgc.com;
 50         charset utf-8;
 51         access_log  logs/www.kgc.com.access.log;
 52         location / {
 53             root   /var/www/html/kgc;
 54             index  index.html index.htm;
 55         }
 56         error_page   500 502 503 504  /50x.html;
 57         location = /50x.html {
 58             root   html;
 59         }
 60     }
 61     # another virtual host using mix of IP-, name-, and port-based c    onfiguration
#多余的内容一直删到这个地方

检查语法,重启服务

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx restart

去win10再测试一下

Nginx虚拟主机之域名,端口,IP
Nginx虚拟主机之域名,端口,IP

Nginx虚拟主机之基于端口

给8080端口写一个网页

[root@localhost conf]# cd /var/www/html/
[root@localhost html]# mkdir kgc8080
[root@localhost html]# echo "this is kgc8080 web" > kgc8080/index.html

配置nginx文件

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

#先把我们之前的cwq域名的注释掉,31,44 s/^/#/g,不然就和下面的地址冲突了
31 #    server {
  #        listen       80;
 33 #        server_name  www.cwq.com;
 34 #        charset utf-8;
 35 #        access_log  logs/www.cwq.com.access.log;
 36 #        location / {
 37 #            root   /var/www/html/cwq;
 38 #            index  index.html index.htm;
 39 #        }  
 40 #        error_page   500 502 503 504  /50x.html;
 41 #        location = /50x.html {
 42 #            root   html;
 43 #        }  
 44 #    }  

46     server {
 47         listen       192.168.136.162:80;
 48         server_name  www.kgc.com;
 49         charset utf-8;
 50         access_log  logs/www.kgc.com.access.log;
 51         location / {
 52             root   /var/www/html/kgc;
 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     }   
 60  
 61     server {
 62         listen       192.168.136.162:8080;
 63         server_name  www.kgc.com;
 64         charset utf-8;
 65         access_log  logs/www.kgc8080.com.access.log;  #不同端口的日志文件
 66         location / {
 67             root   /var/www/html/kgc8080;  #不同端口的站点
 68             index  index.html index.htm;
 69         }
 70         error_page   500 502 503 504  /50x.html;
 71         location = /50x.html {
 72             root   html;
 73         }
 74     }

检查语法,重启服务,查看两个端口有没有被提供出来

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx restart
[root@localhost conf]# netstat -ntap | grep nginx
tcp        0      0 192.168.136.162:8080    0.0.0.0:*               LISTEN      43616/nginx: master 
tcp        0      0 192.168.136.162:80      0.0.0.0:*               LISTEN      43616/nginx: master 

去win10测试一下

Nginx虚拟主机之域名,端口,IP

这是我第二块网卡的网址

Nginx虚拟主机之基于IP地址

我们先给服务器添加一块网卡

Nginx虚拟主机之域名,端口,IP

这是我第二块网卡的网址

ens36: flags=4163  mtu 1500
        inet 192.168.136.171  netmask 255.255.255.0  broadcast 192.168.136.255

kgc区域数据配置文件改个地址就行

[root@localhost ~]# cd /var/named/
[root@localhost named]# vim 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
www IN  A       192.168.136.171

[root@localhost named]# systemctl restart named

用win10测试能不能解析域名成功

Nginx虚拟主机之域名,端口,IP

配置nginx配置文件

[root@localhost named]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
#把之前端口的注释掉,61,74 s/^/#/g
 61 #    server {
 62 #        listen       192.168.136.162:8080;
 63 #        server_name  www.kgc.com;
 64 #        charset utf-8;
 65 #        access_log  logs/www.kgc8080.com.access.log;
 66 #        location / {
 67 #            root   /var/www/html/kgc8080;
 68 #            index  index.html index.htm;
 69 #        }
 70 #        error_page   500 502 503 504  /50x.html;
 71 #        location = /50x.html {
 72 #            root   html;
 73 #        }
 74 #    }
#把前面的注释去掉,31,44 s/#//g
31     server {
        listen       192.168.136.162:80;
 33         server_name  www.cwq.com;
 34         charset utf-8;
 35         access_log  logs/www.cwq.com.access.log;
 36         location / {
 37             root   /var/www/html/cwq;
 38             index  index.html index.htm;
 39         }
 40         error_page   500 502 503 504  /50x.html;
 41         location = /50x.html {
 42             root   html;
 43         }
 44     }
#kgc的区域监听地址换成第二块网卡的地址
 47         listen       192.168.136.171:80;

检查语法,重启服务

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx restart 

再去win10测试一下是不是不同的网址(域名)获得同一个网页

Nginx虚拟主机之域名,端口,IP
Nginx虚拟主机之域名,端口,IP

以上就是我们所有的内容了,谢谢收看

相关内容

热门资讯

原创 硬... 近日,中国华瀛能源果断出手,分别在北京、上海两地法院,起诉摩根大通与花旗集团。起因是两家外资银行盲从...
OTA 升级后续航减 200 ... 系统 OTA 升级后续航从 500km 变 300km?央视揭秘新能源汽车「锁电」真相 5 月 1...
他信假释出狱,小女儿佩通坦到场... 据《曼谷邮报》、路透社报道,当地时间5月11日,泰国前总理他信假释出狱。现场画面显示,他信留着利落短...
特朗普拒绝伊朗方案,油价应声上... 【文/观察者网 王恺雯】美国总统特朗普拒绝伊朗提出的结束战争方案后,国际油价大幅上涨。美东时间5月1...
广东省地震局将发送地震预警科学... 2026年5月12日是第18个全国防灾减灾日。 为让全省公众能及时接收地震预警信息,增强公众防灾减灾...
空调匹数对应多大面积 空调匹数与房间面积的对应关系会受到多种因素的影响,例如房间的朝向、隔热性能、楼层高度、人员数量等。一...
冰柜从哪排水 冰柜是通过排水口进行排水,一般都是在冰柜的最底端的最左侧,打开后面盖子就可以看到那个排水孔了,一般有...
签约服务走过十年 家庭医生如何... 基层群众了解较少、基层医生待遇较低、基层资源落实较难……家庭医生签约服务面临“成长烦恼”签约服务走过...
告诉下风暖浴霸温度达到50度正... 风暖浴霸的温度通常是在20~40度左右,那正常的都是会开到30度左右,差不多也就够用了。如果你家的温...
空调挂机漏水怎么维修 空调挂机是现代生活中必不可少的电器之一,尤其在夏季时更是被广泛使用。但是,如果当你发现你的空调挂机不...