nginx基于域名的虚拟主机配置实战

前言

环境:CentOS 7.9 nginx version: nginx/1.18.0
下面对nginx的配置文件进行设置,配置基于域名的多个虚拟机主机

了解nginx.conf结构

worker_processes  1;									#一个work进程
events {
    worker_connections  1024;							#每个work进程最大能有1024个连接
}
http {													#http块
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {											#虚拟主机server块
        listen       80;								#监听的端口
        server_name  localhost;							#虚拟主机名,可以用IP地址或域名
        location / {									#location块
            root   html;								# root表示站点的根目录是html
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

当配置多个虚拟主机时就是添加多个server块。

基于不同域名配置多个虚拟机主机,共用1个端口

nginx配置三个虚拟主机,分别基于域名fujiseiko_first、fujiseiko_second、fujiseiko_third,监听都用80端口,主机站点目录方别是:html/fujiseiko_first、html/fujiseiko_second、html/fujiseiko_third
先创建这3个目录:cd /usr/local/nginx/html/ && mkdir fujiseiko_first fujiseiko_second fujiseiko_third
在各自目录下创建一个index.html文件,内容分别写上各自的域名即可
开始配置虚拟主机:

worker_processes  1;						
events {
    worker_connections  1024;				
}
http {																	
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {												#第1个虚拟主机															
        listen       80;									#都监听80端口						
        server_name  www.fujiseiko_first.com;				#基于域名www.fujiseiko_first.com					
        location / {														
            root   html/fujiseiko_first;					#根站点位置是html/fujiseiko_first								
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     server {												#第2个虚拟主机															
        listen       80;									#都监听80端口						
        server_name  www.fujiseiko_second.com;				#基于域名www.fujiseiko_second.com					
        location / {														
            root   html/fujiseiko_second ;					#根站点位置是html/fujiseiko_second 
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     server {												#第3个虚拟主机															
        listen       80;									#都监听80端口						
        server_name  www.fujiseiko_third.com;				#基于域名www.fujiseiko_third.com					
        location / {														
            root   html/fujiseiko_third;					#根站点位置是html/fujiseiko_third
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

这样3个基于域名的虚拟主机就配置完了,既然是基于域名,那就要能解析域名,下面在Windows本地域名解析文件C:\Windows\System32\drivers\etc\hosts文件配置域名:
在这里插入图片描述
ping 都能正常解析
在这里插入图片描述

检查语法并重新加载nginx

nginx -t						#修改配置文件后必须检查语法,显示语法OK则表示正常
nginx -s reload					#重新加载配置文件

网页测试

分别输入:http://www.fujiseiko_first.com:80、http://www.fujiseiko_second.com:80、http://www.fujiseiko_third.com:80都能正常显示
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值