Hi all,
I’m trying to run a simple HTTP server on my NixOS machine. I understand that port 80 is reserved for root and it doesn’t seem like the convention is to bind to port 80 directly. The internet says to either a) run a reverse proxy (i.e. nginx) to redirect traffic from port 80 to a higher port or b) use an iptables rule to forward port 80 to a higher port.
Most answers create an iptables rule imperatively, but it seems like there’s a way to do this in a declarative way in the firewall configuration. It also seems like iptables is deprecated? not the convention? I am not really sure.
I found this discourse that seems to describe a recent PR replacing iptables with nftables in the NixOS firewall. I also tried following the wiki guide on port forwarding to forward TCP traffic on port 80 to port 8080.
Here’s my full networking configuration:
networking = {
firewall = {
allowedTCPPorts = [
80 # http
];
allowedUDPPorts = [
];
};
# stuff for web server test
nftables = {
enable = true;
ruleset = ''
table ip nat {
chain PREROUTING {
type nat hook prerouting priority dstnat; policy accept;
tcp dport 80 dnat to :8080
}
}
'';
};
nat = {
enable = true;
externalInterface = "wlp5s0";
forwardPorts = [
{
sourcePort = 80;
proto = "tcp";
destination = "x.x.x.x:8080";
}
];
};
};
where x.x.x.x
is my local IPv4 address, and wlp5s0
is my external interface as reported by ifconfig
. This configuration doesn’t seem to work. I can access the web application on 0.0.0.0:8080 and x.x.x.x
:8080 (when binding the server to 0.0.0.0:8080), but HTTP requests don’t seem to go through on port 80 (i.e. curling the web server on 0.0.0.0:80, x.x.x.x
:80, or typing in my local IP address in the browser).
I am sure I’m missing something obvious, but I haven’t been able to piece together a solution yet. I don’t have the best understanding of networking as a whole. Thanks in advance.