When using proxy_pass, can /etc/hosts be used to resolve domain names instead of "resolver"?
Asked Answered
C

3

39

Can /etc/hosts be used instead of resolver when using proxy_pass?

I need to perform a proxy_pass to the same nginx machine. Is there a way to resolve the domains using the machine's /etc/hosts file instead of specifying a DNS server thru the "resolver" property?

This will save me the additional hops needed to reach the same server. I have tried setting up the internal IP mapped to the DNS in /etc/hosts file but nginx is still reading from the DNS server set in the resolver property. Or is there a way to make the HTTPProxy module to consider the /etc/hosts file settings?

Thanks for any advice you could share..

This is the same question I posted in the nginx forum: http://forum.nginx.org/read.php?11,218997

Cirrostratus answered 29/11, 2011 at 2:14 Comment(3)
The strange thing , this is working if you use upstream (host resolution)Kurtz
Upstream worked for me as well, it's a bit of extra code but it seems cleaner than some of the other methods listed below.Camshaft
It also possible to workaround with systemd-resolved if using a server with systemd. See this answer https://mcmap.net/q/393611/-proxy_pass-does-not-resolve-dns-using-etc-hostsAnnouncement
D
48

You can get around this by installing dnsmasq and setting your resolver to 127.0.0.1. Basically this uses your local DNS as a resolver, but it only resolves what it knows about (among those things is your /etc/hosts) and forwards the rest to your default DNS.

Dewayne answered 19/12, 2011 at 10:12 Comment(4)
But sadly dnsmasq does not automatically detect changes in hosts file. You have to send HUP.Ephemeral
Can you give more details on how we should setup dnsmasq in order to get our nginx to resolve the other container dns name properly ? just installing dnsmasq and adding extra_hosts: - "container_name:127.0.0.1" to my docker-compose.yml config didn't resolve the issueTriggerfish
If you are using docker I think DNS works a little bit different, especially if you're doing container to container communication. Check out the docker networking configuration for more info.Dewayne
Will this work with other *nix flavors (Rasbian specifically) that don't have proxy_pass?Inhaler
K
5

A workaround is to use Nginx map, in order to copy the /etc/hosts content.

map $wanted_host $wanted_host_ip
{
    default 127.0.0.1;
    b.dev.local X.X.X.X;
    a.dev.local X.X.X.X;
}

server
{
    listen              80;
    server_name         ~^(?P<wanted_port>[0-9]+?)-(?P<wanted_host>.+?)\.HOSTNAME$;

    location /
    {
        proxy_pass http://$wanted_host_ip:$wanted_port;

    }
}

This will map wanted_hostto wanted_host_ip , like a resolver.

Kurtz answered 19/7, 2017 at 15:41 Comment(2)
Would this work if has multiple (sub)domains, where just an IP is not enough to know which virtual server to hit?Se
Sure, you could use domain as X.X.X.X, if it resolves.Kurtz
I
0

In my own case on my Windows development and testing machine, I had to install technitium DNS server. Then created a new zone (example.com) and added an A record for my proxy domain's TLD (@) pointing to 127.0.0.1. Then pointed the nginx resolver in the config file to the default Windows system's domain name (desktop-hmvl5656). Note: In my own default installation the technitium DNS server also used the default Windows system domain name for the backend management webportal

location /example {
   resolver desktop-hmvl5656;
   proxy_pass http://example.com$request_uri;
}
Intent answered 21/6, 2024 at 9:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.