本地主机域名
.localhost Domains

原始链接: https://inclouds.space/localhost-domains

我创建了一个系统,使用自定义的`.localhost`域名访问本地Web应用,改进了笨拙的`localhost:port`方法。每个应用都作为launchd守护进程在唯一的端口运行。实现方法如下: 1. 我在`/etc/hosts`文件中将所需的子域名(例如,`appname.localhost`)映射到`127.0.0.1`。 2. Caddy服务器充当反向代理,将来自`appname.localhost`的流量定向到应用的特定端口(例如,5050)。 此设置允许我使用易于记忆的名称(如`inclouds.localhost`)访问应用,而无需记住端口号。虽然功能正常,但配置每个新应用都需要手动编辑三个文件:launchd plist文件、`/etc/hosts`文件和Caddyfile。我的未来目标是使用单个命令自动化整个过程,以便轻松安装和卸载`.localhost`应用。Cristóbal提出的使用`dnsmasq`的建议进一步改进了系统。

这篇 Hacker News 讨论帖谈论了在本地开发中使用 `.localhost` 域名,浏览器现在通常默认将其解析为 `127.0.0.1`,简化了本地设置。几位评论者分享了他们本地域名管理的替代方案。一位用户创建了一个 Docker 代理,将 `.localhost` 解析到容器端口。另一位用户使用子域名 `local.`,并通过 Traefik 获取 Let's Encrypt 证书。使用 `systemd-resolved` 的 Linux 用户发现 `.localhost` 可以开箱即用。该讨论帖警告不要使用 `.local`,因为它与 mDNS/Bonjour 冲突,并建议使用 `.internal` 作为本地网络域名。使用 `.test` 作为替代顶级域名也被提及。一位用户建议在 Safari 中的 `.localhost` URL 后添加尾部斜杠以防止搜索查询。将服务器绑定到 `127.0.0.0/8` 范围内的不同 IP 地址被认为对运行多个 Web 服务器很有用。讨论强调避免域名冲突并为本地开发环境寻找有效的解决方案。
相关文章
  • (评论) 2024-01-28
  • (评论) 2024-08-11
  • (评论) 2024-09-21
  • (评论) 2024-09-20
  • (评论) 2023-10-30

  • 原文

    I’ve found a way to configure private, custom domains for web-apps I have running on my computer. So instead of having to remember and type “localhost:4333”, I can simply navigate to “appname.localhost”. I love it!

    Here’s how the system works:

    1. Each app is set up as a launchd daemon listening on a unique port. Here’s an example plist.

    2. I configure /etc/hosts to redirect traffic to these domains to 127.0.0.1 instead.

    3. I then run and configure Caddy to redirect traffic from 127.0.0.1 to the right port for the domain.

    So, if I’m running an app on port 5050, I will have a corresponding entry in /etc/host:

    127.0.0.1 inclouds.localhost

    and a section in my Caddyfile like this:

    inclouds.localhost {
        reverse_proxy localhost:5050
        tls internal
        encode gzip zstd
    }

    This is working well but I’d really like to see this process simplified. How cool would it be to install or uninstall apps to .localhost domains with a single command, instead of manually editing three files? But that’s a project for another day.

    Thanks for reading!

    Update: cristóbal came up with a command using dnsmasq that makes this even better!

    联系我们 contact @ memedata.com