Getting reverse proxy to work with dynamic request routing

757 views
Skip to first unread message

Duc Nguyen

unread,
Apr 29, 2015, 10:11:52 PM4/29/15
to openre...@googlegroups.com
I took the "Dynamic Request Routing Based on Redis" example from openresty.org and enabled reverse proxy but haven't been able to get the reverse proxy part of it to work. Reverse proxy works fine for hard-coded uri (e.g. http://192.168.1.24:8080/mojo) but won't work when a variable is used (e.g. http://$target). Here's an example:

server {
  listen 192.168.1.24:80;
  server_name  192.168.1.24;

  location /mojo {
    set $target '';
    access_by_lua '
          -- code using redis to lookup dynamic target removed for simplicity, using hard-coded value here
 ngx.var.target = "192.168.1.24:8080/mojo";
    ';

    # reverse proxy setup
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # this works with reverse proxy
    # this does not
    proxy_pass http://$target;
  }
}

I would like to hide the internal hosts/posts from the user but reverse proxy doesn't seem to work when proxy_pass is used with a variable. When it works, user sees http://192.168.1.24/mojo. When it doesn't work, user sees http://192.168.1.24:8080/mojo. Any ideas? I'm really stuck.



David Birdsong

unread,
Apr 29, 2015, 10:32:11 PM4/29/15
to openresty-en
Try setting $target to host and port and use lua's ngx.req.set_uri for the URL path changes.

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Duc Nguyen

unread,
Apr 29, 2015, 11:10:45 PM4/29/15
to openre...@googlegroups.com
Hi David, could you tell me how to set $target to host and port. I just started learning this stuff today so sorry for the stupidity.

David Birdsong

unread,
Apr 30, 2015, 1:08:24 PM4/30/15
to openresty-en
it's a very small modification

<snip>
set $target '';
    access_by_lua '
          -- code using redis to lookup dynamic target removed for simplicity, using hard-coded value here
  ngx.var.target = "192.168.1.24:8080/mojo" -- (removed trailing ';')
          ngx.req.set_uri("/mojo")

    ';
</snip>

note: i haven't tested this, but i recall doing something like this recently


Yichun Zhang (agentzh)

unread,
May 7, 2015, 6:21:01 AM5/7/15
to openresty-en
Hello!

On Thu, Apr 30, 2015 at 10:11 AM, Duc Nguyen wrote:
> Reverse proxy works fine for hard-coded
> uri (e.g. http://192.168.1.24:8080/mojo) but won't work when a variable is
> used (e.g. http://$target).

What do you mean by "won't work"? What exact phenomena are you seeing?
Are you seeing any errors in your nginx error.log file?

I've tried the following standalone and minimal example based on your
sample and it works perfectly on my side:

server {
listen 8080;

location = /t {
set $target '';
access_by_lua '
ngx.var.target = "http://127.0.0.1:8080/mojo"
';
proxy_pass $target;
}

location = /mojo {
echo "hey, there";
}
}

And then query /t gives:

$ curl localhost:8080/t
hey, there

Please note that we should not specify "http://" again in proxy_pass
above because "http://" is already included in the $target variable
value.

BTW, if you use domain names instead of plain IP addresses in the
variable values, then you should also configure the standard
"resolver" configuration directive accordingly:

http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

Best regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages