Configuring Domain Access for Locally Running Services
This page's content used LLM translation.
I often have some services running locally, and accessing their web pages via 127.0.0.1:.INTERNAL as a top-level domain dedicated to internal networks, so I used this domain for the configuration.
DNS
- I need to configure the domain
*.internalto point to 127.0.0.1. Since the method of hacking/etc/hostsdoes not support wildcards, I use dnsmasq to run a DNS service locally.
$ sudo pacman -S dnsmasq
$ sudo systemctl enable dnsmasq
+ Add the domain to the configuration
# /etc/dnsmasq.conf
address=/internal/127.0.0.1
+ A small problem encountered when starting the dnsmasq service: 127.0.0.1:53 is occupied. One solution is to run the service elsewhere in `/etc/dnsmasq.conf`. Remember that you need to configure the DNS address in systemd-resolved. Then restart dnsmasq and systemd-resolved. For various possible solutions, see: [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
I need to configure a web server to point different subdomains to the correct ports. nginx is used here.
Start the nginx service and add a server block to the configuration.
# /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;
}
Restart the nginx service. Now visiting
sub1.internalis redirected to127.0.0.1:3000.A small problem encountered: the
server_nameoption stopped working, and any subdomain was redirected to the same port. The counterintuitive behavior of nginx server_name