代码之家  ›  专栏  ›  技术社区  ›  Kutsan Kaplan

如何在每个人都能看到的本地网络中设置本地域?

  •  15
  • Kutsan Kaplan  · 技术社区  · 7 年前

    我有一些基于web的本地应用程序用于家庭自动化,这些应用程序可以通过IP地址访问,端口号类似 http://192.168.1.100:8080 .

    我试图实现的是将每个单独的IP和端口号组合链接到一个内部域名,这样任何人都可以使用域名和子域名,而不是IP地址。

    http://kitchen.home 而不是具有端口号URL的IP地址,例如 http://192.168.1.100:8080 .

    同时 http://192.168.1.100:8081 hall.home 因此的URL http://hall.home 可以用来代替该IP地址和端口号。

    对服务器的访问不需要修改 hosts

    2 回复  |  直到 4 年前
        1
  •  16
  •   Richard Chambers    4 年前

    我已经在运行 bind9 作为Ubuntu 20.04下的DNS服务器在我的家庭网络上,以便拥有我自己的特殊域名。

    要将域中的多个不同子域名称映射到同一台电脑上的特定端口,您需要在电脑上安装代理服务器以及本地网络的DNS服务器。域名通过DNS协议映射到特定的IP地址,而不是映射到IP地址的特定端口。

    在我的例子中,我有与Ubuntu 20.04相同的PC硬件,其中(1)Bind9用于我的DNS服务器,(2)Apache用于我的代理服务器。我可以使用两台不同的电脑,一台作为DNS服务器,将域名解析为IP地址,另一台提供各种服务,这些服务可以通过单个IP端口访问,使用代理服务器侦听端口,以调度到服务的连接,或通过指定所需服务的正确端口号直接访问服务。

    例如 http://kitchen.home/ kitchen.home 使用端口80,默认HTTP协议端口。DNS服务器用于解析域名 厨房家 到IP地址。URL为 http://kitchen.home:8081/ 厨房家 然后打开到该IP地址的TCP连接,但使用端口8081而不是标准HTTP端口80。

    所以对于 映射到该IP地址处的8080端口 http://hall.home/ 要在该IP地址映射到端口8081,您需要将解析域名的DNS服务器与驻留在端口80(标准HTTP端口)的代理服务器相结合。然后,代理服务器将请求重定向到基于子域名选择的PC上的其他端口, hall kitchen ,表示整个域说明符, 厨房家 hall.home

    看见 https://stackoverflow.com/a/58122704/1466970

    Apache web服务器也可以用作代理服务器,这就是我正在研究的内容。请参阅DigitalOcean的本教程, How To Use Apache HTTP Server As Reverse-Proxy Using mod_proxy Extension Setting up a basic web proxy in apache .

    我的环境

    Ubuntu 20.04 PC是我使用Apache web server的Subversion服务器。我大部分时间都在楼下的Windows 10电脑上,在需要的时候用PuTTY连接到带有一个或多个终端窗口的Ubuntu电脑。我计划在Windows 10 PC上与Visual Studio合作,通过Apache web服务器访问Subversion,并将Ubuntu PC用作数据库服务器(MySQL)、web服务器(Apache with Php)和微服务(golang和node.js)。

    我想在Ubuntu PC上安装一个DNS服务器,然后让我的Windows 10 PC在使用标准DNS服务器(如8.8.8.8和8.8.4.4版本的Google)的同时,为一个特殊的域名使用本地DNS服务器。

    How to Configure BIND9 DNS Server on Ubuntu 20.04 Domain Name Service (DNS) Everything You Need To Know About Ubuntu DNS Servers

    • 安装 绑定9 使用 sudo apt install bind9
    • sudo ufw allow Bind9
    • /etc/bind/named.conf.options
    • 修改文件 /etc/bind/named.conf.local
    • 创建的副本 /etc/bind/db.local home.x
    • 修改新文件, /etc/bind/db.home.x

    我想在本地使用的域名是 如果我输入一个 http://www.home.x/ http://home.x/svn

    为了通过Apache访问Subversion,我必须设置它。看见 enabling Subversion access via Apache web server and DAV on Ubuntu 如果你对此感兴趣。

    更改和修改文件的详细信息

    我添加了一个 forwarders 文件的节 为了将我的Ubuntu服务器未知的任何DNS请求转发到其他DNS服务器。我从Windows 10命令返回的DNS服务器列表中复制的IP地址 ipconfig /all 我在Windows 10 PC上运行的。更改的文件如下:

    rick@rick-MS-7B98:~/Documents$ cat /etc/bind/named.conf.options
    options {
            directory "/var/cache/bind";
    
            // If there is a firewall between you and nameservers you want
            // to talk to, you may need to fix the firewall to allow multiple
            // ports to talk.  See http://www.kb.cert.org/vuls/id/800113
    
            // If your ISP provided one or more IP addresses for stable
            // nameservers, you probably want to use them as forwarders.
            // Uncomment the following block, and insert the addresses replacing
            // the all-0's placeholder.
    
            // forwarders {
            //      0.0.0.0;
            // };
    
            // following forwards are to Google DNS servers at 8.8.8.8 and 8.8.4.4
            forwarders {
                    8.8.8.8;
                    8.8.4.4;
                    209.55.27.13;
            };
    
            //========================================================================
            // If BIND logs error messages about the root key being expired,
            // you will need to update your keys.  See https://www.isc.org/bind-keys
            //========================================================================
            dnssec-validation auto;
    
            listen-on-v6 { any; };
    };
    

    /etc/bind/named。配置选项 ,然后运行检查实用程序,没有发现任何错误,然后重新启动 bind

    sudo named-checkconf
    sudo systemctl restart bind9
    

    接下来,我添加了一个新的 zone /etc/bind/named。配置本地 为我的新本地域名创建DNS条目 . 修改后的文件如下所示:

    rick@rick-MS-7B98:~/Documents$ cat /etc/bind/named.conf.local
    //
    // Do any local configuration here
    //
    
    // Consider adding the 1918 zones here, if they are not used in your
    // organization
    //include "/etc/bind/zones.rfc1918";
    
    zone "home.x" {
            type master;
            file "/etc/bind/db.home.x";
    };
    

    最后,我需要创建文件 /等/绑定/数据库。家x 在中指定 file 的指令 /等/绑定/数据库。地方的 通过使用命令

    sudo cp /etc/bind/db.local /etc/bind/db.home.x
    

    然后我修改了文件 为了指定解析的域名所需的规则 家x www.home.x 到我的Ubuntu PC的IP地址。修改后的文件如下所示:

    rick@rick-MS-7B98:~/Documents$ cat /etc/bind/db.home.x
    ;
    ; BIND data file for local loopback interface
    ;
    $TTL    604800
    @       IN      SOA     home.x. root.home.x. (
                                  2         ; Serial
                             604800         ; Refresh
                              86400         ; Retry
                            2419200         ; Expire
                             604800 )       ; Negative Cache TTL
    ;
    @       IN      NS      ns.home.x.
    @       IN      A       192.168.0.4
    @       IN      AAAA    ::1
    ns      IN      A       192.168.0.4
    www     IN      A       192.168.0.4
    

    在这一点上,我可以通过使用 nslookup 命令窗口中的命令。我先尝试了一下,没有指定Ubuntu PC的IP地址,然后是Ubuntu PC的IP地址

    C:\Users\rickc>nslookup home.x
    Server:  dns.google
    Address:  8.8.8.8
    
    *** dns.google can't find home.x: Non-existent domain
    
    C:\Users\rickc>nslookup home.x 192.168.0.4
    Server:  UnKnown
    Address:  192.168.0.4
    
    Name:    home.x
    Addresses:  ::1
              192.168.0.4
    

    Control Panel > Network and Internet > Network Connections )查看我的网络适配器列表,以修改我的WiFi适配器使用的DNS服务器地址。通过在适配器上单击鼠标右键以弹出浮动菜单,选择属性以打开属性对话框,然后选择Internet协议版本4,然后单击对话框上的属性按钮并修改DNS服务器地址,可以找到此设置。下面是对话框的屏幕截图。

    screen shot of network adapter Properties and modifying DNS server address

    修改DNS服务器地址后,我重试了 nslookup查找 不指定DNS服务器地址,命令将查找 :

    C:\Users\rickc>nslookup home.x
    Server:  UnKnown
    Address:  192.168.0.4
    
    Name:    home.x
    Addresses:  ::1
              192.168.0.4
    

    当我测试的URL 将显示我的Apache web服务器的测试页面。当我测试的URL web浏览器显示了我的Subversion存储库的目录树。当我使用访问Subversion存储库时 http://home.x/svn/trunk/ 在Ankh Subversion插件和Visual Studio 2017中,它可以正常工作。

    这个设置的一个问题是,如果我的Ubuntu PC没有启动并运行,那么Windows 10 PC将不会有正常运行的DNS服务器,除非Ubuntu PC启动,或者DNS服务器地址设置回WiFi适配器上的原始设置。以前,它被设置为发现DNS服务器。也许我可以将其更改回指定的DNS服务器地址,我的Ubuntu PC的IP地址,我的Windows 10 PC将发现我的Ubuntu PC作为DNS服务器。

        2
  •  8
  •   captncraig    7 年前

    DNS名称与端口号无关。dns记录将名称与IP地址相关联。如果要从URL中省略端口号,则需要在默认端口(80/443)上托管,或使用代理。

    1. 也许你的路由器有一个定义主机覆盖的接口。OpenWRT有这样一个功能,一些路由器上有一个类似的dns服务器,您可以将主机条目添加到其中。
    2. A 的记录 example.com 指向本地网络地址。服务器本身可能只能在您的网络内访问,但dns将在任何地方可用。
    3. 运行自己的dns服务器,并告诉路由器将其用作网络的默认解析程序。
    4. mDNS 可以满足您的需求。本地域。