公司中經(jīng)常會搭建一些內(nèi)部的網(wǎng)站,這些網(wǎng)站只能在公司的局域網(wǎng)中使用(鏈接同一個路由器的電腦),當訪問這些網(wǎng)站時需要在瀏覽器中手動輸入服務器ip地址進行訪問,無法通過域名訪問(域名:baidu.com 就是一個域名),這時可以通過搭建公司內(nèi)部的dns服務器解決。
現(xiàn)在想通過訪問 http://mygitlab.com 就可以訪問到位于 192.168.0.100 主機上的自己搭建的gitlab服務器
本文中要搭建的dns服務器和gitlab服務器在同一臺主機上,ip都是192.168.0.100,當然也可以不在同一臺主機上
下載bind9
執(zhí)行如下命令
apt-get install bind9
然后編輯bind9配置文件
找到/etc/bind/name.conf.default-zones文件
并在末尾添加
zone "mygitlab.com" {
type master;
file "/etc/bind/db.ip2mygitlab.com";
};
zone "100.0.168.192.in-addr.arpa" {
type master;
file "/etc/bind/db.mygitlab2ip";
};
zone "mygitlab.com" {
type master;
file "/etc/bind/db.ip2mygitlab.com";
};
zone "100.0.168.192.in-addr.arpa" {
type master;
file "/etc/bind/db.mygitlab2ip";
};
其中100.0.168.192是mygitlab服務器的ip的反寫,若你的服務器ip是abc.def.ghi.jkl則在這要寫成 jkl.ghi.def.abc 。
然后在 /etc/bind 中創(chuàng)建 db.ip2mygitlab.com 文件 和 db.mygitlab2ip 文件,注意這兩個文件名要和 name.conf.default-zones 中配置的file相同
然后在 db.ip2mygitlab.com 文件中填寫如下內(nèi)容,注意 :需要把下面的mygitlab.com換成你的域名,不要漏掉了域名后面的小數(shù)點,192.168.0.100是mygitlab服務器的ip,需要替換成你自己的。
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA ns.mygitlab.com. root.mygitlab.com. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ns.mygitlab.com.
@ IN A 192.168.0.100
ns IN A 192.168.0.100
cn IN A 192.168.0.100
然后在db.mygitlab2ip文件中填寫如下內(nèi)容,同樣要替換mygitlab.com為你的域名。100是mygitlab服務器的ip的最后一段,需要替換成你自己的。
;
; BIND reverse data file for local loopback interface
;
$TTL 604800
@ IN SOA ns.mygitlab.com. root.mygitlab.com. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS mygitlab.com.
1.0.0 IN PTR cn.mygitlab.com.
100 IN PTR mygitlab.com.
100 IN PTR www.mygitlab.com.
100 IN PTR dns.mygitlab.com.
100 IN PTR cn.mygitlab.com.
然后修改 /etc/bind/named.conf.options文件為如下內(nèi)容
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 {
202.101.172.35;
202.101.172.47;
};
//========================================================================
// 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;
auth-nxdomain no; # conform to RFC1035
listen-on-v6 { any; };
};
其實只需要修改
forwarders {
202.101.172.35;
202.101.172.47;
};
這個很關鍵,網(wǎng)上很多人說修改成 8.8.8.8 或 8.8.4.4 或 0.0.0.0,當修改成這三個后會發(fā)現(xiàn)訪問外網(wǎng)特別慢,比如在公司內(nèi)網(wǎng)訪問 baidu.com,就非常慢,訪問任何一個外網(wǎng)都很慢,原因是本地沒有配置外網(wǎng)域名對應的ip,所以 bind9會去8.8.8.8 或 8.8.4.4 或 0.0.0.0這幾個dns服務器查詢ip,本文中修改成了202.101.172.35;
202.101.172.47; 是因為自己搭建的dns服務器位于杭州,用的電信的網(wǎng),所以設置成了 202.101.172.35;
202.101.172.47;,具體設置成多少由你所在地區(qū)的網(wǎng)絡提供上決定(你可以先百度一下ip就可以知道用的是電信還是聯(lián)通還是移動的網(wǎng),然后在百度一下你所在城市對應網(wǎng)絡提供商的dns服務器的ip即可)
然后執(zhí)行sudo service bind9 restart重啟bind9 dns服務器,
可以通過 nslookup 你的域名 查看是否配置成功
分別試了下 mygitlab.com 和 weibo.com ,很快就返回了數(shù)據(jù),若很慢說明 forwarders 中配置的 外網(wǎng)dns服務器有問題。
接下來把DNS服務器IP換成自己搭建的DNS服務器的IP即可。