# Question about using Upstream with proxy\_pass (because want to use /etc/hosts)

**URL:** <https://community.nginx.org/t/question-about-using-upstream-with-proxy-pass-because-want-to-use-etc-hosts/946>\
**Category:** NGINX\
**Tags:** upstream, hostnames, reverse-proxy\
**Created:** [March 4, 2025, 3:10pm UTC](https://community.nginx.org/t/question-about-using-upstream-with-proxy-pass-because-want-to-use-etc-hosts/946 "2025-03-04T15:10:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohaya1001](https://avatars.discourse-cdn.com/v4/letter/o/9fc29f/32.png) [@ohaya1001](https://community.nginx.org/u/ohaya1001)\
**Post date:** [March 4, 2025, 3:10pm UTC](https://community.nginx.org/t/question-about-using-upstream-with-proxy-pass-because-want-to-use-etc-hosts/946/1 "2025-03-04T15:10:25Z")

</div>

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:

[https://stackoverflow.com/questions/8305015/when-using-proxy-pass-can-etc-hosts-be-used-to-resolve-domain-names-instead-of](https://stackoverflow.com/questions/8305015/when-using-proxy-pass-can-etc-hosts-be-used-to-resolve-domain-names-instead-of)

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:

```auto
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:

```auto
   resolver 10.0.0.1;
   upstream localhosttomcat {
        server internaltomcat;
   }

```

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

I changed the proxy\_pass to:

```auto
proxy_pass https://localhosttomcat:34484$request_uri;

```

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

```auto
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:

[https://stackoverflow.com/questions/55475315/nginx-proxy-pass-not-work-with-hosts-file](https://stackoverflow.com/questions/55475315/nginx-proxy-pass-not-work-with-hosts-file)

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… ☹ … so I think my only choice will be to use the IP itself :(…

---

<div class="post-metadata">

**Author:** ![hanada](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/hanada/32/706_2.png) [@hanada](https://community.nginx.org/u/hanada)\
**Post date:** [March 9, 2025, 5:15pm UTC](https://community.nginx.org/t/question-about-using-upstream-with-proxy-pass-because-want-to-use-etc-hosts/946/2 "2025-03-09T17:15:25Z")

</div>

you must set upstream port to upstream block

```auto
   upstream localhosttomcat {
        server internaltomcat:34484;
   }

```

and change proxy\_pass  
`proxy_pass https://localhosttomcat$request_uri;`

---

<div class="post-metadata">

**Author:** ![system](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/system/32/19_2.png) [@system](https://community.nginx.org/u/system)\
**Post date:** [July 3, 2025, 1:04pm UTC](https://community.nginx.org/t/question-about-using-upstream-with-proxy-pass-because-want-to-use-etc-hosts/946/3 "2025-07-03T13:04:30Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
