Question about using Upstream with proxy_pass (because want to use /etc/hosts)

Hi,

Sorry for the longish subject :(!!

So I am in a situation where I need to use /etc/hosts for hostname resolution when I am doing a proxy_pass, but from what I can tell, proxy_pass doesn’t work with /etc/hosts.

So I was Googling and found this:

which mentions that “upstream” supports /etc/hosts and can be used with proxy_pass. I’m fairly new to NGINX and never used ‘upstream’, so wanted to check if I am interpretting things correctly?

So it sounds like if I add something like:

upstream backend {
    server internal.example.com;
}

then I can use backend in a proxy_pass, and it can/wll resolve backend to the entry in the /etc/hosts file that is internal.example.com?

Am I interpreting things correctly?

Thanks in advance!!

Jim

EDIT: So I have tried what I described above.

I modified the default.conf to add the:

   resolver 10.0.0.1;
   upstream localhosttomcat {
        server internaltomcat;
   }

(the “internaltomcat” is in the /etc/hosts)

I changed the proxy_pass to:

proxy_pass https://localhosttomcat:34484$request_uri;

but when I test I am getting 502 error, and in the error.log:

2025/03/04 10:46:48 [error] 3877#3877: *30 localhosttomcat could not be resolved (3: Host not found), client: xxx.yy.124.150, server: tcat.zzzzz.com

Jim

EDIT 2: I think I may’ve found the answer - see this:

the part where it says:

Why it does not work

You are setting proxy_pass http://serverIp:$cookie_testingPort, which means it has a dynamic/variable value. When nginx has a dynamic/variable value for proxy_pass it will try to resolve that domain name in runtime (upon the incoming request) with the help of a dns server and it will ignore /etc/hosts.

So it sounds like, since my proxy_pass has a var for the URI part, then NGINX will not process/check the /etc/hosts file… :frowning: … so I think my only choice will be to use the IP itself :(…

1 Like

you must set upstream port to upstream block

   upstream localhosttomcat {
        server internaltomcat:34484;
   }

and change proxy_pass
proxy_pass https://localhosttomcat$request_uri;