It turned out that ELB is very picky about what he considers to be a βvalidβ answer, and will return 502 Bad Gateway if he is dissatisfied. I fixed this by making sure the response from my server had the following headers:
eg. if i listened to http://example.com
I send the answer:
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 is 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
source share