Websocket适用于EC2网址,但不适用于ElasticBeanstalk网址 [英] Websocket works on EC2 url but not on ElasticBeanstalk URL

查看:126
本文介绍了Websocket适用于EC2网址,但不适用于ElasticBeanstalk网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个反向代理( nginx )指向ElasticBeanstalk( ELB ),它是单实例环境类型,并创建了一个EC2实例( EC2 ).我正在使用dockerized nodejs应用程序. nginx 是我们基础架构的入口点.

I have reverse proxy (nginx) pointing to ElasticBeanstalk (ELB) that is a singleInstance environment type and creates an EC2 instance (EC2). I am using a dockerized nodejs app. nginx is the entry point for our infrastructure.

描述

从浏览器中,我可以直接调用 ELB 的结果相同:

From browser, I can call directly Websocket URLs of an EC2 and of ELB with the same result:

欢迎使用SockJS!

Welcome to SockJS!

对于 nginx ,我使用以下配置( nginx conf.d/my.conf ),其中仅更改 EC2 或<以proxy_pass开头的一行中strong> ELB :

For nginx I use the following config (nginx conf.d/my.conf) where I only change URLs of EC2 or ELB on the line beginning with proxy_pass:

location /stomp {
      proxy_pass <{EC2_URL}/stomp OR {ELB_URL}/stomp>;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
    }

EC2 URL上的

Websocket工作正常.

Websockets on the EC2 URL are just working fine.

ELB URL上的Websockets将错误状态返回给我的客户端:

Websockets on the ELB URL return an error status back to my client:

代码:1002, 原因:无法连接到服务器"

code: 1002, reason: "Cannot connect to server"

最奇怪的是,在 ELB 的情况下,我的 nginx 服务器甚至没有记录请求.

The strangest thing is that my nginx server doesn't even log the requests in case of ELB.

问题

为什么 ELB nginx 配置中不起作用?如何纠正?

Why is the ELB not working in the nginx configuration? How to correct it?

我尝试了什么?

我已经检查了问题和一个博客,他们建议覆盖诸如located in /tmp/deployment/config/etc#nginx#nginx.conf中的00_elastic_beanstalk_proxy.conf之类的nginx配置文件.但是我的服务器在文件系统上没有任何Nginx配置文件...

I have checked a question and a blog where they advice to override nginx config files like 00_elastic_beanstalk_proxy.conf located in located in /tmp/deployment/config/etc#nginx#nginx.conf. But my server doesn't have any nginx configuration files on the filesystem...

推荐答案

问题与您的proxy_set_header Host $host;有关.问题是由于不同服务器对主机名标头的反应方式所致.

The issue is related to your proxy_set_header Host $host;. The issues is because of the way different server reacts to the Host name header.

考虑在浏览器中打开以下网址时

Consider when you open the below urls in browser

http://ec2-52 -59-53-38.eu-central-1.compute.amazonaws.com/stomp http://stag-ws-server.eu-central-1. elasticbeanstalk.com/stomp

一个是EC2,另一个是ELB.当您将请求发送到ec2时,主机名将以ec2-52-59-53-38.eu-central-1.compute.amazonaws.com的形式发送.现在,如果您有直接或通过docker在端口80上侦听的服务.它正在监听端口80,并不关心流量如何到达.

One being EC2 and one being an ELB. When you send the request to ec2 the Host name is sent as ec2-52-59-53-38.eu-central-1.compute.amazonaws.com. Now if you have service which listen on port 80 directly or through docker. It is listening to port 80 and doesn't care how the traffic comes to it.

它只知道如果有人通过任何方式到达服务器,我们就会做出回应.现在,如果您在该EC2上有一个nginx,则它仅侦听特定的虚拟主机,并且不响应任何其他主机名,那么如果您将其发送为ec2-52-59-53-38.eu-central-1.compute.amazonaws.com并且它希望看到abc.mydomain.com,则nginx将不会响应请求.

It just knows that if someone reaches the server by any means we respond. Now if you had a nginx on that EC2, that only listens to a specific Virtual Host and doesn't respond to any other host name, then if you send it ec2-52-59-53-38.eu-central-1.compute.amazonaws.com and it expects to see abc.mydomain.com then nginx wont respond to the request.

您的ELB服务器所发生的情况也是如此.您将nginx托管在某个域名abc.domain.com中,并使用proxy_pass将流量传递给ELB.但是使用proxy_set_header Host $host;,将主机名设置为abc.domain.com,ELB无法理解该主机的位置.所以它不会处理请求,因此不会出错

Same is the case that is happening with your ELB server. You are hosting nginx at some domain name abc.domain.com and using proxy_pass to pass the traffic to ELB. But using proxy_set_header Host $host;, set the host name to abc.domain.com and ELB is not able to understand where that host is. So it wont server the request and hence the error

这篇关于Websocket适用于EC2网址,但不适用于ElasticBeanstalk网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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