# How to use $host variable in proxy\_pass

**URL:** <https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262>\
**Category:** NGINX\
**Created:** [December 28, 2011, 7:43pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262 "2011-12-28T19:43:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [December 28, 2011, 7:43pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262/1 "2011-12-28T19:43:30Z")

</div>

I want to use $host variable in proxy\_pass. If directly hardcode  
Hostname in proxy\_pass it is working fine. But if I use variable $host  
it is giving me “502 Bad Gateway”

server {  
listen 80;  
server\_name “”;

```
    location / {
        proxy_pass https://cds.hh.net; //working fine
        proxy_pass https://$host; //gives error.

```

And $host value is same like above hostname “[cds.hh.net](http://cds.hh.net)”  
proxy\_redirect off;  
}

}

Thanks for help.

Posted at Nginx Forum:

> **[how to use $host variable in proxy\_pass](https://forum.nginx.org/read.php?2,220619,220619#msg-220619)**
>
> I want to use $host variable in proxy\_pass. If directly hardcode Hostname in proxy\_pass it is working fine. But if I use variable $host it is giving me "502 Bad Gateway" server { listen 80; server\_name "";

---

<div class="post-metadata">

**Author:** ![hirenshah.05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@hirenshah.05](https://www.ruby-forum.com/u/hirenshah.05)\
**Post date:** [December 28, 2011, 7:46pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262/2 "2011-12-28T19:46:36Z")

</div>

Hi,

> I want to use $host variable in proxy\_pass. If directly hardcode  
> Hostname in proxy\_pass it is working fine. But if I use variable $host  
> it is giving me “502 Bad Gateway”

Check your error.log, it will tell you that you’re missing “resolver”.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![hirenshah.05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@hirenshah.05](https://www.ruby-forum.com/u/hirenshah.05)\
**Post date:** [December 28, 2011, 8:26pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262/3 "2011-12-28T20:26:46Z")

</div>

what should I specify in resolver?

Posted at Nginx Forum:

> **[Re: how to use $host variable in proxy\_pass](https://forum.nginx.org/read.php?2,220619,220626#msg-220626)**
>
> I want to use $host variable in proxy\_pass. If directly hardcode Hostname in proxy\_pass it is working fine. But if I use variable $host it is giving me "502 Bad Gateway" server { listen 80; server\_name "";

---

<div class="post-metadata">

**Author:** ![hirenshah.05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@hirenshah.05](https://www.ruby-forum.com/u/hirenshah.05)\
**Post date:** [December 28, 2011, 9:10pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262/4 "2011-12-28T21:10:38Z")

</div>

Hi,

> resolver 127.0.0.1;  
> (…)  
> 375#0: recv() failed (111: Connection refused) while resolving,  
> resolver: 127.0.0.1:53

That’s because you don’t have DNS resolver running on the localhost.

Point nginx to the DNS resolver you’re using (from /etc/resolv.conf).

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![hirenshah.05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@hirenshah.05](https://www.ruby-forum.com/u/hirenshah.05)\
**Post date:** [December 28, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262/5 "2011-12-28T20:43:27Z")

</div>

I tried this,

server {  
listen 80;  
server\_name “”;

location / {  
resolver 127.0.0.1;

proxy\_pass https://$host; //gives error. And $host value is same like  
above hostname “[cds.hh.net](http://cds.hh.net)”  
proxy\_redirect off;  
}

}

but still not working. If I check error log it says,

375#0: recv() failed (111: Connection refused) while resolving,  
resolver: 127.0.0.1:53

Thanks for your help.

Hiren

Posted at Nginx Forum:

> **[Re: how to use $host variable in proxy\_pass](https://forum.nginx.org/read.php?2,220619,220627#msg-220627)**
>
> I want to use $host variable in proxy\_pass. If directly hardcode Hostname in proxy\_pass it is working fine. But if I use variable $host it is giving me "502 Bad Gateway" server { listen 80; server\_name "";

---

<div class="post-metadata">

**Author:** ![hirenshah.05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@hirenshah.05](https://www.ruby-forum.com/u/hirenshah.05)\
**Post date:** [December 28, 2011, 9:18pm UTC](https://www.ruby-forum.com/t/how-to-use-host-variable-in-proxy-pass/215262/6 "2011-12-28T21:18:03Z")

</div>

Thanks. It is working now.

Posted at Nginx Forum:

> **[Re: how to use $host variable in proxy\_pass](https://forum.nginx.org/read.php?2,220619,220630#msg-220630)**
>
> I want to use $host variable in proxy\_pass. If directly hardcode Hostname in proxy\_pass it is working fine. But if I use variable $host it is giving me "502 Bad Gateway" server { listen 80; server\_name "";
