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、浏览器访问