搭建nginx虚拟主机——基于域名、端口和IP
admin
2023-03-29 02:41:33
0

nginx支持的虚拟主机有三种

1、基于域名的虚拟主机

2、基于IP的虚拟主机

3、基于端口的虚拟主机


一、基于域名构建

1、编译安装nginx服务

2、配置DNS域名解析服务

3、配置虚拟主机

a、创建自测网页

[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

b、编辑nginx.conf配置文件

vim /usr/local/nginx/conf/nginx.conf
    include conf.d/*.conf;
    server {
        listen             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             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;
        }
    }

c、重载服务

systemctl restart nginx
netstat -ntap | grep 80

d、访问测试

www.kgc.com
www.accp.com

二、基于端口

a、创建另一个端口的测试网页

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

b、编辑nginx.conf配置文件,仅修改监听地址



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

c、重载nginx服务

systemctl restart nginx
netstat -ntap | grep 80

d、测试网页

www.accp.com
www.accp.com8080

三、基于IP

1、修改网页配置文件中的区域数据配置文件

vim /var/named/kgc.com.zone
systemctl restart named

2、编辑nginx.conf中的配置,修改ip地址

 server {
        listen             192.168.109.137: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.109.134: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;
        }
    }

c、重载nginx服务

systemctl restart nginx
netstat -ntap | grep 80

d、测试网页

192.168.109.137
192.168.109.134


相关内容

热门资讯

奢侈品牌MIU MIU多款产品... 澎湃新闻记者 王选辉 实习生 俞涵 罗萱轩继奢侈品牌圣罗兰(Saint Laurent)因对杭州萧山...
河南通报:李建国被开除党籍 经中共河南省委批准,河南省纪委监委对中原资产管理有限公司原党委副书记、副董事长、总经理李建国严重违纪...
河南省纪委监委:李建国严重违纪... 经中共河南省委批准,河南省纪委监委对中原资产管理有限公司原党委副书记、副董事长、总经理李建国严重违纪...
当河南创新装上AI大脑 在全球人工智能浪潮奔涌的当下,中国已跻身全球人工智能发展第一梯队,正处在从并跑向领跑跨越的关键机遇期...
云南砚山警方:王某胜、杨某卫、... 5月9日,云南砚山县公安局发布案件通报称,王某某、杨某某、张某、曾某某等人长期以来纠集百余人在砚山县...
圈层聚散之间:当代青年的社交新... 图片由人机协同生成 当代青年的社交生活已离不开网络。普伦斯基(Marc Prensky)在2001年...
收费的豆包,戳破「AI改造一切... 本文来自微信公众号:壹娱观察(ID: yiyuguancha),文/HAL。 AI时代,地主家也开始...
嘴仗还没完!“是的,我特朗普,... 【文/观察者网 齐倩】美国总统特朗普和教皇利奥十四世之间的口水仗还没完。当地时间5月9日,特朗普转发...
俄乌停火期间互指违规,俄军持续... 据凤凰卫视报道,在5月9日至10日俄乌临时停火期间,俄乌双方均指责对方违反停火协议。乌克兰总参谋部9...
“奥德赛时期”寄托了年轻人的一... 澎湃新闻记者 杨宝宝“事实上奥德修斯并未迷茫过。”《奥德赛》权威译者陈中梅认为,在荷马笔下,奥德修斯...