为什么我会收到一个502网关从后面亚马逊弹性负载平衡器发送重定向时?亚马逊、平衡器、我会、网关

2023-09-11 09:01:14 作者:白蔷薇Queen

我试图重定向任何HTTP请求到我的服务器通过为HTTPS。

I'm trying to redirect any HTTP requests to my server over to HTTPS.

ELB正在侦听端口80和转发所有的请求的端口8088上我的应用程序。然后,应用程序发送一个301永久移动响应重定向到相同的URL,但任何端口剥下来的https://prepended。这将导致客户端重新请求的URL通过HTTPS

ELB is listening on port 80 and forwarding all request to port 8088 on my application. The application then sends a 301 Moved Permanently response redirecting to the same URL, but with any ports stripped off and 'https://' prepended. This causes clients to re-request the url over HTTPS.

当我在本地测试它工作正常,但是当我把它部署到EC2后面的弹性负载均衡,我得到502错误的网关回来。服务器在接收到请求,并似乎正确发送重定向(正如我所说,它工作时,我直接打服务器,而不是通过负载均衡)。

When I test it locally it works fine, but when I deploy it to EC2 behind an Elastic Load Balancer I get 502 Bad Gateway coming back. The server is receiving the request and appears to be sending the redirect correctly (as I said, it works when I hit the server directly, not via a load balancer).

推荐答案

原来,ELB是非常挑剔它认为一个有效的反应,并返回502错误的网关,如果它是不是快乐。我固定它通过确保从我的服务器的响应有以下标题:

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:

如。如果我是在听 http://example.com

我发回如下回应:

HTTP/1.1 301 Moved Permanently
Content-Type: */*; charset="UTF-8"
Location: https://example.com/
Content-Length: 0

这使得ELB快乐,一切正常。

This makes ELB happy and everything works.

有关兴趣,这里的code(Java中,使用 Simpleframework ):

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);
}

同样的code在JavaScript可以在这里找到: https://gist.github.com / dhobbs / 6164710

 
精彩推荐
图片推荐