Search code examples
nginxdnsvpshostsnas

Redirect to internal local ip and port without using /etc/hosts (nginx)


I have a diy (poor man's) NAS and I can access the file-browser in my home-network by using the ip: 192.168.0.2:1111

I could modify the /etc/hosts in each of my devices to redirect my-fancy-filebrowser-url.com to 192.168.0.2:1111.

However, I want to find an alternative that does not involve modifying the /etc/hosts of each device in my network. I do not want to set up a local dns server either as it will probably slow down the resolution of internet domains, I am using 8.8.8.8 or 1.1.1.1 to resolve domain names quicker.

One of those alternatives I found out, is by using nginx. I have purchased a domain name, let's call it mydomain.com and I have an ipv6 VPS server. I have used cloudflare to redirect a url to my server ipv6 address and I have installed nginx to my VPS and I have created this config file:

http {
    # redirect to my router page
    server {
        listen      [d6b6:8760:97ec:ea7a:562c:c954:bb8d:6e41]:80;
        return 302  http://192.168.0.1;
    }
    # redirect to filebrowser
    server {
        listen      [d6b6:8760:97ec:ea7a:562c:c954:bb8d:6e42]:80;
        return 302  http://192.168.0.2:1111;
    }
}

The redirect to my router admin page is working perfectly as expected (for anyone interested I pointed cloudflare subdomain.mydomain.com to the ipv6 address). But the filebrowser one is not. I suspect it is because I am trying to specify a port to redirect to. Is it possible to do something like this with nginx? Or is there any better alternative that does not involve modifying /etc/hosts or setting your own dns server?

Edit: my bad, I was actually inputting the ipv6 address incorrectly in cloudflare. It was missing 1 digit so it was never going to work. I corrected the ip and it works good. The accepted answer does it more cleverly with urls instead of hardcoding the ipv6 which is a good idea! Just note that if you are using a ipv6 server then you are going to listen in the [::]:80 port


Solution

  • Remove the IPv6 addresses in the listen directive and add server_name directives instead:

    http {
        # redirect to my router page
        server {
            listen 80;
            server_name router.mydomain.com;
            return 302 http://192.168.0.1;
        }
        # redirect to filebrowser
        server {
            listen 80;
            server_name filebrowser.mydomain.com;
            return 302 http://192.168.0.2:1111;
        }
    }
    

    I do not want to set up a local dns server either as it will probably slow down the resolution of internet domains

    This is probably a wrong assumption. Something like dnsmasq is able to resolve local names and forward all other DNS queries to upstream servers (like 8.8.8.8 or 1.1.1.1), caching the results. So when setup properly you wouldn't need a domain or a VPS in this case.