事实证明,ELB 对它认为“有效”的响应非常挑剔,如果不满意,它将返回 502 Bad Gateway。我通过确保来自我的服务器的响应具有以下标头来修复它:
例如。如果我在http://example.com上收听
我发回以下回复:
HTTP/1.1 301 Moved Permanently
Content-Type: */*; charset="UTF-8"
Location: https://example.com/
Content-Length: 0
这让 ELB 很高兴,一切正常。
出于兴趣,这是代码(Java,使用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);
}
可以在此处找到 javascript 中的相同代码:https ://gist.github.com/dhobbs/6164710