为什么我从亚马逊弹性负载均衡器后面发送重定向时会得到502坏网关?

Dar*_*ren 5 https http amazon-ec2 amazon-elb

我正在尝试将任何HTTP请求重定向到我的服务器到HTTPS.

ELB正在侦听端口80并将所有请求转发到我的应用程序上的端口8088.然后,应用程序发送301 Moved Permanently响应重定向到同一个URL,但任何端口都被剥离,并且"https://"前置.这会导致客户端通过HTTPS重新请求URL.

当我在本地测试它时工作正常,但是当我将它部署到Elastic Load Balancer后面的EC2时,我会收到502 Bad Gateway.服务器正在接收请求并且似乎正在正确发送重定向(正如我所说,当我直接命中服务器时,它不起作用,而不是通过负载均衡器).

Dar*_*ren 4

事实证明,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
Run Code Online (Sandbox Code Playgroud)

这让 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);
}
Run Code Online (Sandbox Code Playgroud)

相同的 JavaScript 代码可以在这里找到:https://gist.github.com/dhobbs/6164710