Forwarding requests via a proxy

17 views
Skip to first unread message

Nick Barrett

unread,
Sep 8, 2015, 4:06:21 AM9/8/15
to utterlyidle
Hey UtterlyIdle folks, could you provide some advice please on how to forward requests onto another server so as to overcome CORS issues etc in the browser.

I've been messing around with opening url connections with proxies but cant get all the right moving parts.

Lets say all get/post requests for /mysite/hello.html should be forwarded onto http://www.mysite.com:1224/hello.html

Much appreciated in advance!

NIck

Daniel Worthington-Bodart

unread,
Sep 8, 2015, 7:02:23 AM9/8/15
to utter...@googlegroups.com
In nginx you can do something like this:

server {
    listen       80;
    server_name  localhost;

    location / {
        # Location of you static files
        root   /usr/share/nginx/html; 
        index  index.html;
    }

    location /somebackend/ {
        proxy_pass http://server:port/;
        #proxy_set_header SomeHeader "Some Value";
    }
}



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

Daniel Worthington-Bodart

unread,
Sep 8, 2015, 7:09:36 AM9/8/15
to utter...@googlegroups.com
I.e I'd do this with nginx in front of utterlyidle and the other service.

You could do this in utterlyidle as well with something like this:

@Path("mysite")
public class ProxyResource {
    private final HttpClient client;

    public ProxyResource(HttpClient client) {
        this.client = client;
    }

    @ANY
    public Response proxy(Request request, @PathParam("$") String path) {
        return client.handle(modify(request).uri(request.uri().path(path).authority("www.mysite.com:1224")).build());
    }
}

Haven't tested this code, so might not compile or work!




Daniel Worthington-Bodart

unread,
Sep 8, 2015, 7:11:04 AM9/8/15
to utter...@googlegroups.com
Performance would obviously be much better using nginx to reverse proxy as that's what it's designed for!

Stuart Miller

unread,
Sep 8, 2015, 7:11:45 AM9/8/15
to utter...@googlegroups.com

I found a bug when I tried to do this with UtterlyIdle: https://code.google.com/archive/p/utterlyidle/issues/15

Nginx would definitely be the better choice.

Daniel Worthington-Bodart

unread,
Sep 8, 2015, 7:13:11 AM9/8/15
to utter...@googlegroups.com
Don't use the Sun Http Server he has serious production bugs

Stuart Miller

unread,
Sep 8, 2015, 7:15:14 AM9/8/15
to utter...@googlegroups.com

Luckily this was only a scratch project. What are the issues with it?

Daniel Worthington-Bodart

unread,
Sep 8, 2015, 7:16:15 AM9/8/15
to utter...@googlegroups.com

Duplicate requests under load, resource leaks

Alex Luker

unread,
Sep 8, 2015, 8:39:50 AM9/8/15
to utter...@googlegroups.com

Are those problems that came with Java 8?

Only ask cause I remember us having a good experience with it at Sky.

Speaking of duplicate requests, we were using dispatch which was using Apache async-http-client via netty (so many layers). Found out its default behaviour is to retry a request if the connection gets closed - including POSTs. That seemed insane to me, but maybe I've got lost along the way.

Nick Barrett

unread,
Sep 8, 2015, 9:01:43 AM9/8/15
to utterlyidle, d...@bodar.com
Thanks Dan & Stu for responses. Yes nginx could be used - but also wanted to get my uidle version working just out of curiosity (and keeping moving parts to a minimum).

I'm getting this now however which implies I still have something missing:

java.net.MalformedURLException: no protocol: //http://www.blah.co.uk:80/as/dsa/dsa/das/

I also tried adding/removing port and protocol and scheme to no avail. Please feel free to put my out of my misery :-)

Cheers
Nick

Daniel Worthington-Bodart

unread,
Sep 9, 2015, 4:44:48 AM9/9/15
to utter...@googlegroups.com
Send Code!

Daniel Worthington-Bodart

unread,
Sep 9, 2015, 4:54:09 AM9/9/15
to utter...@googlegroups.com
Alex: No the duplicate requests is still there in Java 8 (and previous I imagine), it only happens if your CPU is 100% on the same box as the Sun Http Server. We saw it at Springer when running acceptance tests and another build ran at the same time! We reproduced it by running the "stress" tool on local machines and then we would see duplicate requests in about 1 in 10.

When I arrived at my current client they had been seeing duplicate requests for a while in prod and guess what they were using.

Nick Barrett

unread,
Sep 9, 2015, 5:03:06 AM9/9/15
to utter...@googlegroups.com
Okay, this is the resources code as it stands. 

public class PortalResources {

private final HttpClient httpClient;

public PortalResources(HttpClient httpClient) {
this.httpClient = httpClient;
}

@ANY
@Path("othersite")
public Response proxy(Request request, @PathParam("$") String path) throws Exception {
final Request modifiedRequest = modify(request).uri(request.uri().path(path).authority("http://www.othersite.co.uk")).build();
System.out.println("path was " + path + " modifiedRequest was " + modifiedRequest);
return httpClient.handle(modifiedRequest);
}

}
I think I need to pass a URL to URI as at the moment the URI being generated is //http://www.othersite.co.uk?

which causes java.net.MalformedURLException: no protocol:

Just trying this now but feel free to chip in!

Cheers
Nick

You received this message because you are subscribed to a topic in the Google Groups "utterlyidle" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/utterlyidle/DWsfSy9-GJg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to utterlyidle...@googlegroups.com.

Daniel Worthington-Bodart

unread,
Sep 9, 2015, 6:30:53 AM9/9/15
to utter...@googlegroups.com

Authority can't contain http

Nick Barrett

unread,
Sep 9, 2015, 7:45:42 AM9/9/15
to utter...@googlegroups.com
Actually that didn’t work but this did:

final Request modifiedRequest = modify(request)
.uri(uri(new URL("http://www.othersite.co.uk")).mergePath(path))
.build();
Reply all
Reply to author
Forward
0 new messages