It turns out that ELB is very picky about what it considers a 'valid' response and will return 502 Bad Gateway if it isn't happy. I fixed it by making sure the response from my server had the following headers:
eg. if I was listening at http://example.com
I send back the following response:
HTTP/1.1 301 Moved Permanently
Content-Type: */*; charset="UTF-8"
Location: https://example.com/
Content-Length: 0
This makes ELB happy and everything works.
For interest, here's the code (Java, using Simpleframework):
private static void startHttpsRedirector() throws IOException {
org.simpleframework.http.core.Container container = new org.simpleframework.http.core.Container() {
@Override
public void handle(Request request, Response response) {
Path path = request.getPath();
Query query = request.getQuery();
String rawHost = request.getValue("host");
System.out.println("Raw host: " + rawHost);
System.out.println("Raw path: " + path);
System.out.println("Raw query: " + query);
String host = rawHost.replaceFirst("\\:.*", "");
response.setStatus(Status.MOVED_PERMANENTLY);
String redirectTo = "https://" + host + path + (query.values().size() > 0 ? "?" + query : "");
System.out.println("redirectTo = " + redirectTo);
response.setContentType("*/*; charset=\"UTF-8\"");
response.setValue("Location", redirectTo);
response.setContentLength(0);
try {
response.commit();
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Server server = new ContainerServer(container);
Connection connection = new SocketConnection(server);
SocketAddress address = new InetSocketAddress(8088);
connection.connect(address);
}
The same code in javascript can be found here: https://gist.github.com/dhobbs/6164710