nginx 二、配置域名


docker中启动nginx容器完成如下操作,对于docker安装nginx可以看这篇文章
nginx 一、安装与conf浅析

一、配置本地域名

hosts文件路径C:\Windows\System32\drivers\etc。
修改hosts,如果没有权限,可以copy到有权限的路径,修改完成后再copy到C:\Windows\System32\drivers\etc进行覆盖。

查看虚拟机ip

获取虚拟机地址,用于hosts中配置域名对应的ip

ifconfig

在这里插入图片描述

修改hosts文件

# 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

二、配置aliyun域名

在这里插入图片描述

进入域名解析界面,添加记录

  • 记录类型:我们是IPV4的地址,所以选择A即可,如果是其他的需求例如邮件可以选择MX,文本选择TXT,DNS可以选择NS
  • 主机记录:根据需要进行选择
    www:解析后的域名为www.aliyun.com。
    @:直接解析主域名 aliyun.com。
    *:泛解析,匹配其他所有域名 *.aliyun.com。
    mail:将域名解析为mail.aliyun.com,通常用于解析邮箱服务器。
    二级域名:如:abc.aliyun.com,填写abc。
    手机网站:如:m.aliyun.com,填写m。
    显性URL:不支持泛解析(泛解析:将所有子域名解析到同一地址)
  • 记录值:填写对应的服务器ip
  • TTL:超时时间

最后确认即可创建好域名。

三、实践

1.创建html

# 我直接把index.html放到这个路径下了
cd /home/test/nginx/conf/conf.d/
# 创建html,随便写点东西即可,我写test
vi index.html

2.配置nginx

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

3.测试

任意一种方式测试均可:

服务器内部测试

curl test.com
#返回index.html中写的内容

页面测试

浏览器直接访问test.com即可
在这里插入图片描述

总结

通过以上的步骤,我们可以成功地配置本地和阿里云域名,并进行相应的测试来验证配置是否正确。
在实际操作中,需要注意以下几点:

  • 确认虚拟机 IP 地址和服务器 IP 地址,以便正确设置 hosts 文件和阿里云域名解析。

  • 在修改 Nginx 的配置文件时,需要确保文件路径和内容正确无误。

  • 在进行测试时,可以使用命令行工具或浏览器进行测试,检查返回的页面内容是否正确。

总之,正确配置域名是搭建网站的必要步骤之一,也是网站正常运行的重要保障。希望以上内容能够对你有所帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈年小趴菜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值