Redirect traffic based on url to different ip address maintaining port and url information [duplicate]

Solution 1:

I have established what the problems were with my setup.

1) SELinux was preventing me from connecting upstream. I have now disabled this and will consider setting it up properly later

2) proxy_pass was doing its job as expected, however the args I needed were http://123.123.123.1:$server_port/$uri$is_args$args;

3) proxy_set_header Host $host correctly set the hostname back to what I wanted, however it ate the port number. The correct format for my needs is proxy_set_header Host $host:$server_port

There may be neater solutions to this, and I haven't got a full working solution yet as I have cut it down to get it working, however a working section of my config is:

server {
    listen 41002;
    server_name uat.comapny.com;

    location /webapp {
        proxy_pass http://123.123.123.1:41002/$uri$is_args$args;
        proxy_set_header Host $host:$server_port;
    }
}

I will post a more generic version once I have fleshed it out. Many thanks to all who helped.

Solution 2:

You want to maintain the port, then you should put it in proxy_pass.

server {

    server_name bob.something.com;

    listen 41001;
    listen 41002;
    listen 8080;

    location / {
            proxy_pass http://123.123.123.1:$server_port$uri;
            proxy_set_header Host $host;
    }
}

You have to realize that this will proxy request through nginx.