Nginx虚拟主机配置实例
admin
2023-02-22 13:40:07
0次

Nginx虚拟主机

结合上篇文章:手工编译NginxNginx虚拟主机的搭建过程,虚拟主机的概念在之前的Apache虚拟主机搭建实验时已讲述过有关知识点,原文链接:Apache web 虚拟主机

结合上篇文章的配置进行下面的配置操作(Nginx服务是开启状态)

[root@localhost named]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 79214/nginx: master

Nginx虚拟主机配置

1.域名解析配置(环境准备)

[root@localhost ~]# yum install -y bind
...//省略部分内容
  dhclient.x86_64 12:4.2.5-77.el7.centos                                         
  dhcp-common.x86_64 12:4.2.5-77.el7.centos                                      
  dhcp-libs.x86_64 12:4.2.5-77.el7.centos                                        

Complete!
[root@localhost ~]# vim /etc/named.conf 
[root@localhost ~]# head -21 /etc/named.conf |tail 
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 
[root@localhost ~]# vim /etc/named.rfc1912.zones 
[root@localhost ~]# head -34 /etc/named.rfc1912.zones | tail 
zone "ll.com" IN {
        type master;
        file "ll.com.zone";
        allow-update { none; };
};

zone "cc.com" IN {
        type master;
        file "cc.com.zone";
        allow-update { none; };
[root@localhost ~]# 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 ll.com.zone
[root@localhost named]# vim ll.com.zone 
[root@localhost named]# cp -p ll.com.zone cc.com.zone
[root@localhost named]# cat ll.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.68.144
[root@localhost named]# cat cc.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.68.144

[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@localhost named]# setenforce 0

2.在win10虚拟机上使用nslookup命令测试是否正常解析

Nginx虚拟主机配置实例

3.创建站点

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

4.基于不同域名的服务解析设置

[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost conf]# sed -n '35,63p' nginx.conf
    server {
        listen       80;
        server_name  www.ll.com;
        charset utf-8;
        access_log  logs/www.ll.com.access.log;
        location / {
            root   /var/html/ll;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        server_name  www.cc.com;
        charset utf-8;
        access_log  logs/www.cc.com.access.log;
        location / {
            root   /var/html/cc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
[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 

5.不同域名的测试

Nginx虚拟主机配置实例

Nginx虚拟主机配置实例

Nginx基于不同端口访问

继续根据上面的第四步的配置

[root@localhost conf]# sed -n '35,63p' nginx.conf
    server {
        listen       192.168.68.144:80;
        server_name  www.ll.com;
        charset utf-8;
        access_log  logs/www.ll.com.access.log;
        location / {
            root   /var/www/html/ll;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen      192.168.68.144:8080;
        server_name  www.cc.com;
        charset utf-8;
        access_log  logs/www.cc8080.com.access.log;
        location / {
            root   /var/www/html/cc8080;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     nginx -t
[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 

检测:

Nginx虚拟主机配置实例

Nginx基于不同IP地址访问

添加一块网卡选择nat模式

我的是192.168.68.150

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

[root@localhost conf]# vim /var/named/cc.com.zone 
[root@localhost conf]# cat /var/named/cc.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.68.150
[root@localhost conf]# systemctl restart named

查看解析是否成功:

Nginx虚拟主机配置实例

2.更改配置文件

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# sed -n '35,63p' nginx.conf
    server {
        listen      192.168.68.144:80;
        server_name  www.ll.com;
        charset utf-8;
        access_log  logs/www.ll.com.access.log;
        location / {
            root   /var/www/html/ll;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen      192.168.68.150:80;
        server_name  www.cc.com;
        charset utf-8;
        access_log  logs/www.cc.com.access.log;
        location / {
            root   /var/www/html/cc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
[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 

检查测试:

Nginx虚拟主机配置实例

Nginx虚拟主机配置实例

总结

本文主要是通过Nginx手工编译安装的基础上对Nginx的虚拟主机的相关配置,分别对应的是基于不同域名、不同端口和不同ip进行的相关配置。重要的是对Nginx的配置文件nginx.conf的配置。这里的域名解析的相关配置需要比较娴熟。

下一篇我们将介绍LNMP架构的搭建过程

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...