Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)
admin
2023-02-28 01:41:49
0

DNS服务配置

1.安装bind服务

[root@localhost sbin]# yum install bind -y
...........//省略安装过程
[root@localhost sbin]#

2.查看网卡信息(IP地址)

[root@localhost named]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.52.133  netmask 255.255.255.0  broadcast 192.168.52.255
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)
        RX packets 384057  bytes 558603083 (532.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 182891  bytes 11237471 (10.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3.修改主配置文件

[root@localhost sbin]# 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 sbin]#

4.修改区域配置文件

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

zone "abc.com" IN {        //添加两个区域信息
        type master;
        file "abc.com.zone";
        allow-update { none; };
};

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

[root@localhost sbin]#

5.修改区域数据配置文件

[root@localhost sbin]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost abc.com.zone    //复制模板并命名
[root@localhost named]# vim abc.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.52.133   //添加解析地址

[root@localhost named]# cp -p abc.com.zone xyz.com.zone    //复制abc域名的区域配置文件命名为xyz域名
[root@localhost named]# ls
abc.com.zone  data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves  xyz.com.zone
[root@localhost named]# 
[root@localhost named]# systemctl start named   //开启dns服务
[root@localhost named]# systemctl stop firewalld.service    //关闭防火墙
[root@localhost named]# setenforce 0    //关闭增强性安全功能
[root@localhost named]# 

基于域名的虚拟主机配置

1.分别给两个站点创建首页文件

[root@localhost named]# mkdir -p /var/www/html/abc    //创建abc站点
[root@localhost named]# mkdir -p /var/www/html/xyz    //创建xyz站点
[root@localhost named]# cd /var/www/html/
[root@localhost html]# ls
abc  xyz
[root@localhost html]# echo "this is abc web" > abc/index.html   //创建首页文件
[root@localhost html]# echo "this is xyz web" > xyz/index.html   //创建首页文件
[root@localhost html]#

2.修改nginx服务配置文件

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

    server {
        listen       80;             //监听端口
        server_name  www.abc.com;      //域名
        charset utf-8;      //字符集,utf-8支持中文字符
        access_log  logs/www.abc.com.access.log;     //访问日志
        location / {
            root   /var/www/html/abc;    //站点
            index  index.html index.htm;     //支持的首页类型
        }
        error_page   500 502 503 504  /50x.html;    //访问错误文件
        location = /50x.html {
            root   html;     //站点
        }
    }

    server {
        listen       80;    //监听端口
        server_name  www.xyz.com;     //域名
        charset utf-8;      //字符集,utf-8支持中文字符
        access_log  logs/www.xyz.com.access.log;    //访问日志
        location / { 
            root   /var/www/html/xyz;    //站点
            index  index.html index.htm;     //支持的首页类型
        }
        error_page   500 502 503 504  /50x.html;    //访问错误文件
        location = /50x.html {
            root   html;     //站点
        }
    } 

[root@localhost html]#

3.检查测试配置文件,并重启服务

[root@localhost html]# 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 html]# service nginx restart      //重启服务
[root@localhost html]# 

4.开启一台win10作为测试机,设置dns地址

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

5.测试能否进行域名解析(成功)

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

6.用测试机浏览器访问两个域名

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

基于端口的虚拟主机配置

1.修改nginx服务的配置文件

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

    server {
        listen       192.168.52.133:80;     //监听端口
        server_name  www.abc.com;
        charset utf-8;
        access_log  logs/www.abc.com.access.log;    //访问日志
        location / {
            root   /var/www/html/abc;     //站点
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       192.168.52.133:8080;    //监听端口
        server_name  www.abc.com;
        charset utf-8;
        access_log  logs/www.abc8080.com.access.log;     //访问日志
        location / {
            root   /var/www/html/abc8080;     //站点
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

2.测试配置文件

[root@localhost html]# 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 html]# 

3.创建8080端口站点目录与首页文件

[root@localhost html]# mkdir abc8080   //创建站点目录
[root@localhost html]# echo "this is abc8080 web" > abc8080/index.html     //创建首页文件
[root@localhost html]# service nginx restart    //重启nginx服务
[root@localhost html]#

4.用测试机浏览器访问相同域名的不同端口

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

基于IP的虚拟主机配置

1.给虚拟机添加一块网卡

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

2.查看网卡信息(IP地址)

[root@localhost html]# ifconfig 
ens33: flags=4163  mtu 1500
        inet 192.168.52.133  netmask 255.255.255.0  broadcast 192.168.52.255
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)
        RX packets 391887  bytes 559453355 (533.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 185573  bytes 11520948 (10.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163  mtu 1500
        inet 192.168.52.139  netmask 255.255.255.0  broadcast 192.168.52.255
        inet6 fe80::f7fb:4ddc:f4b6:b90a  prefixlen 64  scopeid 0x20
        ether 00:0c:29:27:1c:49  txqueuelen 1000  (Ethernet)
        RX packets 14  bytes 1737 (1.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 4219 (4.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3.修改域名区域数据配置文件“xyz.com.zone”的解析地址

[root@localhost html]# vim /var/named/xyz.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.52.139    //修改ip地址为192.168.52.139

[root@localhost html]# systemctl restart named    //重启dns服务
[root@localhost html]# 

4.用测试机检查域名解析是否正常(正常)

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

5.修改nginx服务的配置文件

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

    server {
        listen       192.168.52.133:80;     //监听IP地址与端口
        server_name  www.abc.com;    //域名
        charset utf-8;    //字符集,utf-8支持中文字符
        access_log  logs/www.abc.com.access.log;    //访问日志
        location / { 
            root   /var/www/html/abc;    //站点
            index  index.html index.htm;   //支持的首页格式
        }
        error_page   500 502 503 504  /50x.html;    //访问错误首页文件
        location = /50x.html {
            root   html;      //站点
        }
    } 

    server {
        listen       192.168.52.139:80;     //监听IP地址与端口
        server_name  www.xyz.com;    //域名
        charset utf-8;    //字符集,utf-8支持中文字符
        access_log  logs/www.xyz.com.access.log;    //访问日志
        location / {
            root   /var/www/html/xyz;    //站点
            index  index.html index.htm;   //支持的首页格式
        }
        error_page   500 502 503 504  /50x.html;    //访问错误首页文件
        location = /50x.html {
            root   html;      //站点
        }
    }

6.测试配置文件,没有问题,重启服务

[root@localhost html]# 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 html]# service nginx restart    //重启服务
[root@localhost html]# 

7.用测试机分别访问两个不同IP地址的域名

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

Nginx服务虚拟主机的配置----------基于域名、端口、IP(实战!)

相关内容

热门资讯

郑丽文:只有中国国民党可以确保... 据凤凰卫视报道,国民党今天(7月25日)举行“全代会”,党主席郑丽文与党内提名的县市长参选人一同造势...
透视上半年消费外贸外资走势 新华社北京7月23日电 题:稳中提质 韧性增强——透视上半年消费外贸外资走势新华社记者谢希瑶、王聿昊...
水龙头出水小怎么解决 水龙头出水小可能是由多种原因引起的,以下是一些常见的解决方法:1. 清洁水龙头:水龙头使用时间长了,...
小神童洗衣机启动后不出水直接洗 有可能是洗衣机的进水阀被烧坏了,需要换一个新的进水阀安装上即可。另外不建议自己擅自拆卸洗衣机内部进行...
祝贺景德镇! 海报制作:苗夏阳正在韩国釜山举行的联合国教科文组织第48届世界遗产大会25日通过决议将“景德镇手工瓷...
中央空调不制冷怎么办-中央空调... 中央空调,是电器股份有限公司主要产品之一,但是由于中央空调使用环境和使用方式的不同,在使用过程中就会...
空调不出水不制冷怎么回事 内罩长期不清洗,当然房间密封性差的话也会导致这种现象;其次就是外机问题,如果外机积满灰尘,会成空调制...
空调不出水,不制冷 原因有多种,可能是电源插头没有接牢、制冷剂不足、空调短时间休息间隙、空调滤网积灰等等。很多情况可以自...
中方反击了,欧盟回应 据凤凰卫视报道,中国商务部7月24日宣布对包括德国军工巨头莱茵金属公司在内的14家欧洲企业实施出口管...
这个“中国首次”,很有看点 刚刚,韩国釜山,第48届世界遗产大会传来好消息:“景德镇手工瓷业遗存”成功列入《世界遗产名录》!这是...