Nginx – http redirect and append client IP as URL query

haproxynginxquerystringredirect

i have this weird issue which we need as a workaoround for an internal application limitation.

this is what I need, either from haproxy or nginx:

  • something calls http://my-server/?some=query_string
  • the server should respond with a redirect (http 301/302) to this location: http://my-other-server/?some=query_string?client_ip=ACTUAL_CLIENT_IP

could somebody provide an idea about how this might work? or even better still – the DNS reverse lookup name for the client IP?

thanks in advance!
axel.

Best Answer

You could try this:

location / {
    return 301 http://my-other-server/?client_ip=$remote_addr&$args;
}

This will assign the IP address. https://github.com/flant/nginx-http-rdns is a reverse DNS lookup module for nginx, which provides $rdns_hostname variable, which you can use instead of $remote_addr. However, you need to compile the module to your nginx to use it.