hosts文件路径C:\Windows\System32\drivers\etc。
修改hosts,如果没有权限,可以copy到有权限的路径,修改完成后再copy到C:\Windows\System32\drivers\etc进行覆盖。
获取虚拟机地址,用于hosts中配置域名对应的ip
ifconfig
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
在hosts文件最后增加ip和域名即可
172.27.64.169 test.com
通过ping命令进行测试域名配置是否成功,ping通即为成功
root@DESKTOP-5BAJU6R:/home/test# ping test.com
PING test.com (172.27.64.169) 56(84) bytes of data.
64 bytes from test.com (172.27.64.169): icmp_seq=1 ttl=64 time=2.37 ms
64 bytes from test.com (172.27.64.169): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from test.com (172.27.64.169): icmp_seq=3 ttl=64 time=0.042 ms
64 bytes from test.com (172.27.64.169): icmp_seq=4 ttl=64 time=0.040 ms
64 bytes from test.com (172.27.64.169): icmp_seq=5 ttl=64 time=0.039 ms
64 bytes from test.com (172.27.64.169): icmp_seq=6 ttl=64 time=0.038 ms
进入域名解析界面,添加记录
最后确认即可创建好域名。
# 我直接把index.html放到这个路径下了
cd /home/test/nginx/conf/conf.d/
# 创建html,随便写点东西即可,我写test
vi index.html
cd /home/test/nginx/conf/conf.d/
# 创建配置文件
vi test.conf
配置文件内容如下
server{
listen 80;
server_name test.com;
# 配置docker容器内部地址
root /etc/nginx/conf.d/;
location /{
index index.html;
}
}
划重点,一定要记着重启nginx
docker restart nginx
任意一种方式测试均可:
curl test.com
#返回index.html中写的内容
浏览器直接访问test.com即可
通过以上的步骤,我们可以成功地配置本地和阿里云域名,并进行相应的测试来验证配置是否正确。
在实际操作中,需要注意以下几点:
确认虚拟机 IP 地址和服务器 IP 地址,以便正确设置 hosts 文件和阿里云域名解析。
在修改 Nginx 的配置文件时,需要确保文件路径和内容正确无误。
在进行测试时,可以使用命令行工具或浏览器进行测试,检查返回的页面内容是否正确。
总之,正确配置域名是搭建网站的必要步骤之一,也是网站正常运行的重要保障。希望以上内容能够对你有所帮助。