Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

proxy_pass does not resolve DNS using /etc/hosts

In nginx the proxy_pass directive is not resolving DNS using the /etc/hosts file. Any way to enable this possibility (maybe by leveraging Lua as a last resort)?

like image 738
Mark Avatar asked May 01 '15 02:05

Mark


People also ask

Does etc host override DNS?

The /etc/hosts file contains a mapping of IP addresses to URLs. Your browser uses entries in the /etc/hosts file to override the IP-address-to-URL mapping returned by a DNS server. This is useful for testing DNS (domain name system) changes and the SSL configuration before making a website live.

Does nslookup use etc hosts?

The nslookup utility in Red Hat, SuSE and other Linux operating systems can only resolve hostnames or IP addresses that are in a DNS database. nslookup is not coded to consult the /etc/nsswitch. conf file, and it is not able to resolve hostnames or addresses that are in the local /etc/hosts file.

Does Nginx use etc hosts?

Its default installation folder is /etc/nginx. in /etc/hosts, nginx will receive our requests for baeldung.com and direct those to the webserver running on 127.0. 0.1:8080.


2 Answers

In short, you need to install dnsmasq locally and use 127.0.0.1 as a resolver.

See this related question and answer:

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

like image 118
Aiman Alsari Avatar answered Oct 19 '22 13:10

Aiman Alsari


If you have a server with systemd you can use the nameserver shipped with it, systemd-resolved, to resolve your hostnames from the /etc/hosts file.

Just add this to your nginx config http, server or location block:

resolver 127.0.0.53;

Use systemctl status systemd-resolved.service to see if it running on your server.

like image 42
Epeli Avatar answered Oct 19 '22 15:10

Epeli