# Nginx: dynamic proxy\_pass

**URL:** <https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008>\
**Category:** General Discussion\
**Tags:** config, reverse-proxy, web-server\
**Created:** [February 11, 2026, 7:39am UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008 "2026-02-11T07:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim-king-2000](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/jim-king-2000/32/4136_2.png) [@jim-king-2000](https://community.nginx.org/u/jim-king-2000)\
**Post date:** [February 11, 2026, 7:39am UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008/1 "2026-02-11T07:39:36Z")

</div>

I want to use nginx for a dynamic proxy. Here are the rules:

```auto
/os-abc/... to http://abc.a.domain.com:1234/...
/os-xxx/... to http://xxx.a.domain.com:1234/...
/os-yyy/... to http://yyy.a.domain.com:1234/...
...

```

So I write a proxy rule in nginx.conf.

```auto
location ~ ^/os-(.*)/ {
    proxy_pass http://$1.a.domain.com:1234/;

    add_header Cache-Control 'public, max-age=99999999999, immutable';
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

```

But it does **NOT** work (get 502). nginx version: nginx/1.28.2

# My Question

1. How could I see what `http://$1.a.domain.com:1234/` becomes?

2. How to let it work?

3. Why is this simple requirement so hard to implement?

# Progress

I add resolver directive to the conf file. But the error log shows: `hdd1 could not be resolved (3: Host not found)`. It seems nginx force to resolve $1 instead of “$[1.a.domain.com](http://1.a.domain.com)”.

---

<div class="post-metadata">

**Author:** ![jim-king-2000](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/jim-king-2000/32/4136_2.png) [@jim-king-2000](https://community.nginx.org/u/jim-king-2000)\
**Post date:** [February 11, 2026, 9:24am UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008/2 "2026-02-11T09:24:01Z")

</div>

Get it work. The correct conf:

```auto
location ~ ^/os-(?<subdomain>[^/]+)(?<path>/.*)?$ {
        proxy_pass http://$subdomain.abc.com:8060$path$is_args$args;

        proxy_set_header Host $subdomain.abc.com;
}

```

Some points:

1. Wrong regex. Use `[^/]+` instead of `.*`.
2. Wrone Host. Use `$subdomain.abc.com` instead of `$host`.
3. Must concatenate path manually when using regex. Use `http://$subdomain.abc.com:8060$path$is_args$args` instead of `http://$1.a.domain.com:1234/`.
4. Can’t see the backend url. Very hard to debug.

# Have to say

The rule of conf is **TOO** tricky.

---

<div class="post-metadata">

**Author:** ![buulam](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/buulam/32/3733_2.png) [@buulam](https://community.nginx.org/u/buulam)\
**Post date:** [February 11, 2026, 9:10pm UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008/3 "2026-02-11T21:10:43Z")

</div>

Thank you for sharing your solve @jim-king-2000 !

---

<div class="post-metadata">

**Author:** ![jim-king-2000](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/jim-king-2000/32/4136_2.png) [@jim-king-2000](https://community.nginx.org/u/jim-king-2000)\
**Post date:** [February 12, 2026, 1:52am UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008/4 "2026-02-12T01:52:49Z")

</div>

It took me 2 days to debug it. It would be much easier if I could see the proxyed url. Hope nginx would add it to the log. Or new users would repeat my failure.

---

<div class="post-metadata">

**Author:** ![buulam](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/buulam/32/3733_2.png) [@buulam](https://community.nginx.org/u/buulam)\
**Post date:** [February 12, 2026, 1:03pm UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008/5 "2026-02-12T13:03:31Z")

</div>

We can look into this. Thank you for highlighting.

---

<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:** [February 14, 2026, 1:04pm UTC](https://community.nginx.org/t/nginx-dynamic-proxy-pass/9008/6 "2026-02-14T13:04:09Z")

</div>

Closed on February 14, 2026, 1:04pm UTC

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