# Can I proxy\_pass to a variable?

**URL:** <https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314>\
**Category:** NGINX\
**Created:** [May 10, 2010, 5:55am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314 "2010-05-10T05:55:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 10, 2010, 5:55am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314/1 "2010-05-10T05:55:46Z")

</div>

config:  
server {  
listen [::]:80 default;  
server\_name \_ ;  
server\_name\_in\_redirect off;  
resolver 74.207.241.5;

```
    access_log off;

    auth_basic "Private proxy";
    auth_basic_user_file /etc/nginx/auth_basic_user;

    location /{
            proxy_pass http://$http_host;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;  
}  
}

error.log:  
2010/05/10 11:02:25 [error] 31032#0: \*45 zero length URI to proxy,  
client: ::ffff:124.205.200.99, server: \_, request: “GET  
[http://www.whatismyip.com/](http://www.whatismyip.com/) HTTP/1.1”, host: “[www.whatismyip.com](http://www.whatismyip.com)”

If a specify an IP address or a domain name in proxy\_pass, it worked.  
I use $http\_host, it doesnt work. How can I do?

# /usr/sbin/nginx -V

nginx version: nginx/0.7.64  
TLS SNI support enabled  
configure arguments: --conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock  
–http-log-path=/var/log/nginx/access.log  
–http-client-body-temp-path=/var/lib/nginx/body  
–http-proxy-temp-path=/var/lib/nginx/proxy  
–http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug  
–with-http\_stub\_status\_module --with-http\_flv\_module  
–with-http\_ssl\_module --with-http\_dav\_module  
–with-http\_gzip\_static\_module --with-mail --with-mail\_ssl\_module  
–with-ipv6  
–add-module=/tmp/buildd/nginx-0.7.64/modules/nginx-upstream-fair

–  
Ren Xiaolei

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 10, 2010, 8:02am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314/2 "2010-05-10T08:02:11Z")

</div>

On Mon, May 10, 2010 at 7:54 AM, ä»»æ™“ç£Š [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â Â Â Â auth\_basic\_user\_file /etc/nginx/auth\_basic\_user;
> 
> Â Â Â Â location /{

- Â Â Â Â Â Â Â Â proxy\_pass http://$http\_host;

- 

```
           proxy_pass http://$http_host$uri;

```

> –http-proxy-temp-path=/var/lib/nginx/proxy  
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Boris D..

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 10, 2010, 8:08am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314/3 "2010-05-10T08:08:02Z")

</div>

On Mon, May 10, 2010 at 11:54:45AM +0800, ä»»æ™“ç£Š wrote:

> ```
> auth_basic_user_file /etc/nginx/auth_basic_user;
> 
> ```
> 
> client: ::ffff:124.205.200.99, server: \_, request: “GET  
> [http://www.whatismyip.com/](http://www.whatismyip.com/) HTTP/1.1”, host: “[www.whatismyip.com](http://www.whatismyip.com)”
> 
> If a specify an IP address or a domain name in proxy\_pass, it worked.  
> I use $http\_host, it doesnt work. How can I do?
> 
> # /usr/sbin/nginx -V
> 
> nginx version: nginx/0.7.64

You have to write:

```
 proxy_pass http://$http_host$request_uri;

```

Since 0.8.25 and 0.7.65, you may write:

```
 proxy_pass http://$http_host;

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 10, 2010, 10:30am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314/4 "2010-05-10T10:30:51Z")

</div>

On Mon, May 10, 2010 at 2:07 PM, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You have to write:
> 
> Â Â proxy\_pass http://$http\_host$request\_uri;
> 
> Since 0.8.25 and 0.7.65, you may write:
> 
> Â Â proxy\_pass http://$http\_host;

But, why didn’t I need to specify $request\_uri when proxy\_pass to a  
literal value?

–  
Ren Xiaolei

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 10, 2010, 10:52am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314/5 "2010-05-10T10:52:30Z")

</div>

On Mon, May 10, 2010 at 04:29:46PM +0800, ä»»æ™“ç£Š wrote:

> literal value?  
> For historical reasons.  
> There are two ways to map URI to a resource: root and alias:

```
 location /dir1/ {
     root /path/to;
 }

 location /dir2/ {
     alias /path/to/dir3/;
 }

```

The variables support has appeared eventually:

```
 location / {
     root /path/$host;
 }

 location ~ ^/~(?<user>[^/]+)(?<path>.*)$ {
     alias /homes/$user/$path;
 }

```

In proxy\_pass the root way is:

```
 location /dir1/ {
     proxy_pass http://host;
 }

```

The alias way is:

```
 location /dir2/ {
     proxy_pass http://host/dir3/;
 }

```

The proxy\_pass variables support has appeared to support proxying to a  
full  
URL only as in static alias. Then the root way has been added.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 10, 2010, 11:01am UTC](https://www.ruby-forum.com/t/can-i-proxy-pass-to-a-variable/188314/6 "2010-05-10T11:01:18Z")

</div>

On Mon, May 10, 2010 at 4:52 PM, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The proxy\_pass variables support has appeared to support proxying to a full  
> URL only as in static alias. Then the root way has been added.  
> Thank you for explaination.

–  
Ren Xiaolei
