nginx 二、配置域名

文章目录

  • 一、配置本地域名
    • 查看虚拟机ip
    • 修改hosts文件
    • 测试域名是否配置成功
    • 二、配置aliyun域名
    • 三、实践
      • 1.创建html
      • 2.配置nginx
      • 3.测试
        • 服务器内部测试
        • 页面测试
        • 总结

          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 的配置文件时,需要确保文件路径和内容正确无误。

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

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