本地经常会运行着一些服务,每次通过 127.0.0.1: 的方式访问 web 页面显得太过笨拙。刚好前几日 ICANN 保留了 .INTERNAL 作为内网专用的顶级域名,就使用这个域名来配置。

DNS

  • 需要配置域名 *.internal 指向 127.0.0.1。由于魔改 /etc/hosts 的方法并不支持 wildcard,使用 dnsmasq 在本地起一个 DNS 服务。
$ sudo pacman -S dnsmasq
$ sudo systemctl enable dnsmasq
+ 在配置中添加域名
# /etc/dnsmasq.conf

address=/internal/127.0.0.1
+ 启动 dnsmasq 服务时遇到的小问题:127.0.0.1:53 被占用。一种解决方案是在 `/etc/dnsmasq.conf` 中把服务运行到其他地方。记得需要在 systemd-resolved 中配置 DNS 地址。之后重启 dnsmasq 和 systemd-resolved。各种可能的解决方案参考:[link](https://askubuntu.com/questions/191226/dnsmasq-failed-to-create-listening-socket-for-port-53-address-already-in-use)
# /etc/dnsmasq.conf
listen-address=127.0.53.1
bind-interfaces


# /etc/systemd/resolved.conf
[Resolve]
DNS=127.0.53.1

Web Server

  • 需要配置一个 web server,将不同的子域名指向正确的端口。这里使用 nginx。

  • 启动 nginx 服务,在配置中添加 server 块。

# /etc/nginx/nginx.conf

http {
	include conf.d/*.conf;
}

# /etc/nginx/conf.d/internal.conf

server {
    listen 80;
    server_name sub1.internal;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

server {
    listen 80;
    server_name *.internal;
    
	return 404;
}
  • 重启 nginx 服务。现在访问 sub1.internal 即被重定向到 127.0.0.1:3000。

  • 遇到的小问题:server_name 选项失效,任何子域名都会被重定向到同一个端口。 nginx server_name 的反常理行为