【问题标题】:Using rest server and websocket with elasticbeanstalk, java, cannot connect使用带有elasticbeanstalk,java的rest服务器和websocket,无法连接
【发布时间】:2017-08-28 16:02:16
【问题描述】:

我正在尝试将 nginx 路由配置为能够同时使用休息服务器(使用 Java Spark)和 Websockets(使用 Netty-socketIO)。

它在本地运行良好,但无法在 aws elasticbeanstalk 上运行。

我在端口 5000 上侦听 Java Spark,这是 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html 的默认端口

默认情况下,Elastic Beanstalk 将 nginx 代理配置为将请求转发到端口 5000 上的应用程序

这行得通。

我在端口 9000 上监听 Websocket。我确实将 ELB 协议更改为 TCP。

仍然来自 aws 文档:

要扩展 Elastic Beanstalk 的默认 nginx 配置,请将 .conf 配置文件添加到应用程序源包中名为 .ebextensions/nginx/conf.d/ 的文件夹中。 Elastic Beanstalk 的 nginx 配置自动在此文件夹中包含 .conf 文件。

我试过但没有成功:

server {
    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

SocketIO-客户端连接字符串http://beanstalk-address-here.us-east-1.elasticbeanstalk.com:9000

在“网络”选项卡中,请求在失败前处于(待处理)一段时间。

【问题讨论】:

  • 您是否启用了目标群体的粘性?没有它,我猜网络套接字无法工作

标签: java nginx websocket socket.io amazon-elastic-beanstalk


【解决方案1】:

一个可能的问题是您没有正确配置您的 ELB,除了端口 9000 上的连接。您必须在 ELB 上正确配置安全组以允许非标准端口上的连接:

https://aws.amazon.com/premiumsupport/knowledge-center/elb-connectivity-troubleshooting/

【讨论】:

    【解决方案2】:

    如果你仍然通过非标准端口访问你的应用程序,你为什么要使用 nginx

    Nginx 路由通常在一个端口(通常是 80/443)中接受所有连接,并且根据配置路径,它会重定向到任何特定的 ip/端口

    你的 nginx 配置文件应该有这些

    Nginx 监听端口

       server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  localhost;
    
       }
    

    重定向列表

    server{
    ....
    
    location / {
            proxy_pass http://<your-app-ip>:<your-app-port>;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;   
    }  
    
    }
    

    现在重启 nginx

    您的应用网址是 http://beanstalk-address-here.us-east-1.elasticbeanstalk.com:9000

    你的重定向url是http://beanstalk-address-here.us-east-1.elasticbeanstalk.com(nginx会监听80端口并重定向到你的应用端口)

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2018-10-20
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多