域名解析体系中 IPv4/IPv6 地址切换的关键技术剖析

在这里插入图片描述

前言:
对接的一家学校业务,学校老师要求域名解析既能解析到ipv4地址又能解析到ipv6地址。听学校老师叙述(还是会考察v6开通率的),所以通过这个方法来实现的,域名解析到ipv6和ipv4都可以。

准备一台机器

机器配置:CPU:2C/内存:3G/磁盘:40G
机器系统:Anolis OS 8.6

部署两个tomcat服务

#下载安装包
wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.99/bin/apache-tomcat-8.5.99.tar.gz
#安装openjdk
yum -y install java-1.8.0-openjdk.x86_64
#检查
java -version
================安装tomcat1操作==============================
#解压tomcat
tar xf apache-tomcat-8.5.99.tar.gz
#重命名
mv apache-tomcat-8.5.99 tomcat1
#修改tomcat配置
#打开 Tomcat 安装目录下的conf文件夹,找到server.xml文件。
#在server.xml中找到<Host>标签,通常在该标签内添加以下内容来指定默认访问的 Web 应用
cd /home/app/tomcat1/conf/
cp server.xml server.xml.bak
vim /home/app/tomcat1/conf/server.xml
<Context path="" docBase="www" debug="0" reloadable="true" />
#这里的path=""表示根路径,即访问http://your_ip:8080/时会直接访问www应用;docBase指定了应用程序的目录,即前面放置页面文件的目录。
vim /home/app/tomcat1/webapps/www/index.html
<h2>11111111</h2>
/home/app/tomcat1/bin/startup.sh

=================安装tomcat2操作====================
#解压tomcat
tar xf apache-tomcat-8.5.99.tar.gz
#重命名
mv apache-tomcat-8.5.99 tomcat2
#修改tomcat配置
#打开 Tomcat 安装目录下的conf文件夹,找到server.xml文件。
#在server.xml中找到<Host>标签,通常在该标签内添加以下内容来指定默认访问的 Web 应用
cd /home/app/tomcat2/conf/
cp server.xml server.xml.bak
vim /home/app/tomcat2/conf/server.xml
<Context path="" docBase="www" debug="0" reloadable="true" />
#这里的path=""表示根路径,即访问http://your_ip:8080/时会直接访问www应用;docBase指定了应用程序的目录,即前面放置页面文件的目录。
#tomcat2修改端口号,以免上面tomcat1端口号冲突.
vim /home/app/tomcat2/webapps/www/index.html
<h2>22222222</h2>
/home/app/tomcat2/bin/startup.sh

部署nginx服务并配置

#安装nginx
yum -y install nginx
#配置nginx
[root@test-servre ~]# cat /etc/nginx/conf.d/test.conf
upstream test{
   server 127.0.0.1:8081;
   server 127.0.0.1:8080;
}
server
{
        listen 80;
        listen       [::]:80;
        server_name _;

          location / {
         proxy_pass http://test;
         proxy_set_header  X-Real-IP  $remote_addr;
         proxy_set_header Host $host;
             proxy_redirect off;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_connect_timeout 300s;
             proxy_send_timeout 300s;
             proxy_read_timeout 300s;
             proxy_buffer_size       16k;
         proxy_buffers           4 64k;
         proxy_busy_buffers_size 128k;
         proxy_temp_file_write_size 128k;
         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
         }

        location ~ /\.
        {
            deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                                    root   html;
                    }
                    location /RequestDenied {
                                 return 405;
                    }
                    error_page  405  /405.html;
                    location = /405.html {
                                    root   html;
                    }
}
#将nginx主配置文件这段注释掉
cp nginx.conf nginx.conf.bak
vim nginx.conf
 #   server {
 #       listen       80 default_server;
 #       listen       [::]:80 default_server;
 #       server_name  _;
 #       root         /usr/share/nginx/html;

 #       # Load configuration files for the default server block.
 #       include /etc/nginx/default.d/*.conf;

 #       location / {
 #       }

 #       error_page 404 /404.html;
 #           location = /40x.html {
 #       }

 #       error_page 500 502 503 504 /50x.html;
 #           location = /50x.html {
 #       }
 #   }

#启动nginx服务
systemctl start nginx
#检查
systemctl status nginx 
#测试使用把域名解析到ipv4地址上进行访问
[root@test-servre ~]# cat  /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.253.129 test.com

#ping域名测试
[root@test-servre ~]# ping -c3 test.com
PING test.com (192.168.253.129) 56(84) bytes of data.
64 bytes from test.com (192.168.253.129): icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from test.com (192.168.253.129): icmp_seq=2 ttl=64 time=0.068 ms
64 bytes from test.com (192.168.253.129): icmp_seq=3 ttl=64 time=0.037 ms

--- test.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2034ms
rtt min/avg/max/mdev = 0.037/0.076/0.123/0.035 ms

#访问测试
[root@test-servre ~]# curl http://test.com
<h2>22222222</h2>
[root@test-servre ~]# curl http://test.com
<h2>11111111</h2>

#域名解析到ipv4,访问域名过程
[root@test-servre ~]# curl -v test.com
* Rebuilt URL to: test.com/
*   Trying 192.168.253.129...
* TCP_NODELAY set
* Connected to test.com (192.168.253.129) port 80 (#0)
> GET / HTTP/1.1
> Host: test.com
> User-Agent: curl/7.61.1
> Accept: */*
>
< HTTP/1.1 200
< Server: nginx/1.14.1
< Date: Wed, 23 Apr 2025 02:54:06 GMT
< Content-Type: text/html
< Content-Length: 18
< Connection: keep-alive
< Accept-Ranges: bytes
< ETag: W/"18-1745308189839"
< Last-Modified: Tue, 22 Apr 2025 07:49:49 GMT
<
<h2>22222222</h2>
* Connection #0 to host test.com left intact

新增ipv6网卡信息

#在ipv4网卡下面新增ipv6网卡信息
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33.bak
vim ifcfg-ens33

IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
# 静态 IPv6 地址及前缀长度
IPV6ADDR=2001:db8:1234:5678::1/64
# IPv6 网关
IPV6_DEFAULTGW=2001:db8:1234:5678::ff
# IPv6 DNS 服务器
IPV6_DNS1=2001:4860:4860::8888
IPV6_DNS2=2001:4860:4860::8844
#重启网卡
ifdown ens33 && ifup ens33
#测试ipv6地址是否正常通
[root@test-servre ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.253.129  netmask 255.255.255.0  broadcast 192.168.253.255
        inet6 2001:db8:1234:5678::1  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:e9:7c:dc  txqueuelen 1000  (Ethernet)
        RX packets 264920  bytes 359401377 (342.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 65021  bytes 6249142 (5.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@test-servre ~]# ping -6 -c3 2001:db8:1234:5678::1
PING 2001:db8:1234:5678::1(2001:db8:1234:5678::1) 56 data bytes
64 bytes from 2001:db8:1234:5678::1: icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from 2001:db8:1234:5678::1: icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from 2001:db8:1234:5678::1: icmp_seq=3 ttl=64 time=0.076 ms

--- 2001:db8:1234:5678::1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2032ms
rtt min/avg/max/mdev = 0.039/0.052/0.076/0.017 ms

#域名解析到ipv6上进行访问测试
[root@test-servre ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
2001:db8:1234:5678::1 test.com

#ping域名是否解析到ipv6上
[root@test-servre ~]# ping -c3 test.com
PING test.com(test.com (2001:db8:1234:5678::1)) 56 data bytes
64 bytes from test.com (2001:db8:1234:5678::1): icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from test.com (2001:db8:1234:5678::1): icmp_seq=2 ttl=64 time=0.089 ms
64 bytes from test.com (2001:db8:1234:5678::1): icmp_seq=3 ttl=64 time=0.096 ms

--- test.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2050ms
rtt min/avg/max/mdev = 0.088/0.091/0.096/0.003 ms

#访问域名测试
[root@test-servre ~]# curl http://test.com
<h2>22222222</h2>
[root@test-servre ~]# curl http://test.com
<h2>11111111</h2>

#域名解析到ipv6,访问域名过程
[root@test-servre ~]# curl -v test.com
* Rebuilt URL to: test.com/
*   Trying 2001:db8:1234:5678::1...
* TCP_NODELAY set
* Connected to test.com (2001:db8:1234:5678::1) port 80 (#0)
> GET / HTTP/1.1
> Host: test.com
> User-Agent: curl/7.61.1
> Accept: */*
>
< HTTP/1.1 200
< Server: nginx/1.14.1
< Date: Wed, 23 Apr 2025 02:53:08 GMT
< Content-Type: text/html
< Content-Length: 18
< Connection: keep-alive
< Accept-Ranges: bytes
< ETag: W/"18-1745308066686"
< Last-Modified: Tue, 22 Apr 2025 07:47:46 GMT
<
<h2>11111111</h2>
* Connection #0 to host test.com left intact

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值