504 网关超时 - 两个带有负载均衡器的 EC2 实例 [英] 504 Gateway Timeout - Two EC2 instances with load balancer

查看:21
本文介绍了504 网关超时 - 两个带有负载均衡器的 EC2 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是不可能的问题.我什么都试过了.我觉得某个地方的总机前有个人在不停地盘着小胡子.

This might be the impossible issue. I've tried everything. I feel like there's a guy at a switchboard somewhere, twirling his mustache.

问题:

我让 Amazon EC2 运行一个应用程序.当只有一个实例且没有负载均衡器时,它可以正常运行.

I have Amazon EC2 running an application. It functions without issue when there is only one instance and no load balancer.

但在我的生产环境中,我有两个相同的实例在一个负载平衡器后面运行,并且在执行某些任务时,例如生成 PDF 并将其附加到电子邮件的功能,什么也没有发生,并且在使用 Google 开发人员工具时使用网络"选项卡,一旦超时到达,我就会收到错误504 网关超时"(我将其设置为 30 秒).

But in my production environment I have two identical instances running behind one load-balancer and when performing certain tasks, like a feature that generates a PDF and attaches it to an email, nothing happens at all, and when using Google Developer tools with the Network tab I get the error "504 Gateway Timeout" once the timeout hits (I have it set at 30 seconds).

我的数据库是外部的,在 Amazon RDS 上.

My Database is external, on Amazon RDS.

我想....如果我可以强制客户端保持连接到他们登录的初始服务器,这个问题就会得到解决,因为据我了解,当 instance-1 尝试执行时,504 网关超时正在发生联系实例 2 以执行任务.

I think.... If I could force a client to stay connected to their initial server they logged in at, this problem would be solved, because it's my understanding that the 504 Gateway Timeout is happening when instance-1 tries to reach out to instance-2 to perform the task.

这种情况仅在使用负载平衡时发生,但在直接连接到我的两台服务器之一时不会发生.

This happens ONLY WHEN using Load Balancing, but never when connecting straight to one of my two servers.

负载均衡器设置:

  • 负载均衡器在我的注册器上有一个 CRECORD,因此 app.myapplication.com 指向 myloadbalancerDNSname.elb.amazonaws.com
  • 负载均衡器有 2 个运行状况良好的实例,每个实例位于同一区域,但位于不同的可用区.
  • 负载均衡器使用与实例相同的安全组(允许端口 22、80 和 443 上的所有 IP)
  • 负载平衡器已开启跨区域负载平衡.
  • CORS(在 Amazon S3 中)可以从 * 到 * 进行 GET、POST、PUT、DELETE(我不知道这与我的实例有何关联,但无论如何我按照说明进行了操作)
  • 负载均衡器的监听器配置如下:
    • 负载均衡器协议:HTTP 负载均衡器端口:80 实例协议:HTTP 实例端口:80
    • 负载均衡器协议:HTTPS 负载均衡器端口:443 实例协议:HTTP 实例端口:80(根据我的证书提供商正确选择的密码,并且 SSL 字段 100% 肯定正确)

    更多想法:

    话虽如此,我不是在使用 HTTPS 进行测试,而是使用普通的 HTTP 进行测试.我不相信 SSL 设置正确,即使我的证书提供商说它是.我怀疑的原因是当我尝试输入 https://app.myapplication.com 时,我得到Google Developer Tools 中的(失败)net::ERR_CONNECTION_CLOSED"错误,在网络"选项卡中.但这应该不适用,因为即使使用常规 HTTP 我也遇到问题.我可以稍后对 SSL 进行故障排除.

    That being said, I'm not testing with HTTPS, but normal HTTP instead. I'm not convinced SSL is setup properly even though my certificate provider said it is. The reason I'm suspicious is that when I try to key in https://app.myapplication.com I get the error "(failed) net::ERR_CONNECTION_CLOSED" in Google Developer Tools, in the Network tab. But this should be non-applicable because I'm having the problem even using regular HTTP. I can troubleshoot SSL later.

    所以重申一下,我的问题是在使用某些功能时出现504网关超时"问题,但偶尔也会随机而不是加载页面(但很少).这个 504 问题仅在使用负载平衡时发生,但在直接连接到我的两个实例之一时不会发生.

    So to reiterate, my problem is having the "504 Gateway Timeout" problem when using some functions, but also occasionally at random instead of loading the page (but rarely). This 504 problem happens ONLY WHEN using Load Balancing, but never when connecting straight to one of my two instances.

    我不知道该问哪个问题,因为我已经将每个文档都跟踪到了 T,两次和三次检查了网络上的所有建议,但什么也没有.

    I don't know which question to ask, because I've Followed every document to the T, double and triple checked all suggestions all over the web and NOTHING.

    推荐答案

    就我而言,负载均衡器没有问题.最终的解决方案是 Ubuntu 的主机文件,其中有一个莫名其妙的条目将流量从某个神秘 IP 路由到我的应用程序的主机名.因此,在创建 PDF 的过程中,路径被 PDF 生成器重写以指向神秘服务器,因此网关超时问题.我不知道为什么它偶尔会工作并且不会失败.

    In my case, it turns out that there was no problem with the load balancer. The final solution ending up being Ubuntu's hosts file in which there was an inexplicable entry to route traffic from some mystery IP to my application's host name. So, during the process of creating the PDF, paths were getting re-written by the PDF generator to point at the mystery server, and hence the Gateway timeout issues. I have no idea why it was occasionally working and not failing.

    127.0.0.1 localhost
    127.0.1.1 ubuntu-server
    42.139.126.191 app.myapp.com
    

    这就是它的样子,所以我去掉了第三条线,所有的齿轮又开始转动了.:P

    This is what it looked like, so I removed that third line and all the gears started turning again. :P

    这篇关于504 网关超时 - 两个带有负载均衡器的 EC2 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆