Nginx网络服务-002篇,Nginx基于域名的虚拟主机

Nginx网络服务-002篇,Nginx基于域名的虚拟主机

注意:
此处使用hosts文件代替DNS域名解析,实际的企业级环境中,一定是基于DNS来管理你的域名系统的。

1、Nginx基于域名的虚拟主机配置文件

[root@hanyw-nginx202 conf]# cat /usr/local/nginx/conf/nginx.conf_domain_vhosts.bak 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        #server_name  localhost;
        server_name  blog.hanyw.com;
        location / {
            root   html/blog.hanyw.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }



    server {
        listen       80;
        #server_name  localhost;
        server_name  img.hanyw.com;
        location / {
            root   html/img.hanyw.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        #server_name  localhost;
        server_name  video.hanyw.com;
        location / {
            root   html/video.hanyw.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }




}

2、 Nginx虚拟主机配置步骤【网站服务器端】

2.1 、服务器端配置域名解析(host文件替代)

[root@hanyw-nginx202 conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

### 2024-05-24-10:14:12 hanyw ###
192.168.80.202 blog.hanyw.com
192.168.80.202 img.hanyw.com
192.168.80.202 video.hanyw.com
### 2024-05-24-10:14:12 hanyw ###

2.2、确认域名解析配置是否正确

[root@hanyw-nginx202 conf]# ping -c 2 blog.hanyw.com
PING blog.hanyw.com (192.168.80.202) 56(84) bytes of data.
64 bytes from blog.hanyw.com (192.168.80.202): icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from blog.hanyw.com (192.168.80.202): icmp_seq=2 ttl=64 time=0.105 ms

--- blog.hanyw.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1045ms
rtt min/avg/max/mdev = 0.016/0.060/0.105/0.044 ms
[root@hanyw-nginx202 conf]# ping -c 2 img.hanyw.com
PING img.hanyw.com (192.168.80.202) 56(84) bytes of data.
64 bytes from blog.hanyw.com (192.168.80.202): icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from blog.hanyw.com (192.168.80.202): icmp_seq=2 ttl=64 time=0.086 ms

--- img.hanyw.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1069ms
rtt min/avg/max/mdev = 0.016/0.051/0.086/0.035 ms
[root@hanyw-nginx202 conf]# ping -c 2 video.hanyw.com
PING video.hanyw.com (192.168.80.202) 56(84) bytes of data.
64 bytes from blog.hanyw.com (192.168.80.202): icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from blog.hanyw.com (192.168.80.202): icmp_seq=2 ttl=64 time=0.098 ms

--- video.hanyw.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1067ms
rtt min/avg/max/mdev = 0.016/0.057/0.098/0.041 ms

2.3、确认防火墙是否被关闭

[root@hanyw-nginx202 conf]# sestatus 
SELinux status:                 disabled
[root@hanyw-nginx202 conf]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:firewalld(1)

3、Windows端配置hosts解析【浏览器客户端】

3.1、微标键+r,输入drivers,打开hosts文件

在这里插入图片描述

或者微标键+r输入如下路径

C:\Windows\System32\drivers\etc

3.2、hosts文件内容

在这里插入图片描述

3.3、Windows客户端测试域名

3.3.1、进入cmd命令行模式

进入cmd命令行模式,如下图所示。
在这里插入图片描述

3.3.2、测试域名和IP地址的解析记录是否正常

测试域名和IP地址的的解析记录是否正确
在这里插入图片描述

4、Nginx服务器端,创建网页首页测试文件

4.1、创建网站默认首页文件

echo "<h1>blog.hanyw.com</h1>" > blog.hanyw.com/index.html
echo "<h1>video.hanyw.com</h1>" > video.hanyw.com/index.html
echo "<h1>img.hanyw.com</h1>" > img.hanyw.com/index.html

4.2、浏览器访问

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

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux云计算架构师-韩艳威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值