1

Is it possible to configure nginx to query a service (not DNS) to determine the proxy_pass destination based on the currently-requested URI?

For example:

  1. Client requests http://somesite.tld/somepage, which needs to be proxied.
  2. nginx connects to the resolving service and sends the request URI which is used to determine the proxy destination.
  3. The resolving service responds, providing the hostname of the destination.
  4. The client's request is redirected to the given destination.

If this is not supported, what would be a good way to do this? I could proxy_pass to a resolver+proxy, but would prefer to let nginx handle the proxying directly to the destination. Note that the resolver needs to be queried because the destinations for each URI may change.

Thanks :-)

0

3 Answers 3

1

I don't see a way to achieve what you are asking.

However if you are simply looking to map request args to hostnames (and they don't change a lot), then you could probably use Nginx's map module.

You might also be able to do something using mod_lua, but it would likely be terribly hackish.

Sign up to request clarification or add additional context in comments.

1 Comment

mod_lua looks promising -- possibly using ngx.location.capture to perform the lookup and set a variable with the destination host.
0

This is fully supported from the box. Use domain name in the *proxy_pass* directive. For example:

proxy_pass http://yourproxy:port;

After that you could configure your DNS and change yourproxy address as you wish.

2 Comments

Thanks for the reply. I'm looking for a way that doesn't use DNS, but rather something simpler to write a server for (such as HTTP). Can nginx make an HTTP request and pull a value from the response into a variable? That variable could then be used in the proxy_pass destination.
It's very strange & hack configuration. This will better to take a look on SSI or use internal header X-Accel-Redirect for you . Actually you can use variable in proxy_pass directive, but with standard modules you couldn't save request to variable.
0

@Pavel Did you have a chanсe to configure nginx as described? I have a similar task: before proxying a request it has to be defined which server can handle it. So far I see only two approches of how this could be done: either to implement a custom module for nginx (looking at the sources of how 'load balance' is done) or use an application server and put together an application that will perform forwarding of requests.

The latter approach seems simplier, but I'm not sure about performance.

Thanks.

2 Comments

I agree with the two options you've listed. I went with the application server approach -- very simple to implement and works great. If it was for absolutely enormous traffic, I'd consider a custom nginx module.
@Pavel, I have contacted nginx developers and they recommended to use X-Accel-Redirect. The only thing to do is to respond (in the header) where the request should be redirected to. I can confirm it works perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.