【问题标题】:Sails.js Socket Connection 400 Bad RequestSails.js 套接字连接 400 错误请求
【发布时间】:2017-07-09 23:25:12
【问题描述】:

应用程序位于 aws(elasticbeanstack) 上的负载均衡器后面。在负载平衡之前它已经工作了。应用程序响应 400 Bad Request。关于这个话题有很多问题。我尝试了很多。粘性会话,将上游代理更改为http://localhost,连接升级......但我无法弄清楚。这是 nginx 错误日志:

*1217 连接()失败(111:连接 拒绝)同时连接到上游,客户端:172.31.31.90,服务器:, 请求:“获取 /socket.io/?__sails_io_sdk_version=0.13.8&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=LfS5yzc&sid=i1QLZeltxMS0D5oBAAAE HTTP/1.1",上游: "http://127.0.0.1:8081/socket.io/?__sails_io_sdk_version=0.13.8&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=LfS5yzc&sid=i1QLZeltxMS0D5oBAAAE"

Nginx 配置;

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections 1024;
    multi_accept on;
}

http {

    port_in_redirect off;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    # Cache modifikasyonları
    open_file_cache          max=10000 inactive=10m;
    open_file_cache_valid    20m;
    open_file_cache_min_uses 1;
    open_file_cache_errors   on;

    # Transfers
    client_body_buffer_size 10K;            # client POST buffer size
    client_header_buffer_size 1k;           # client header size
    client_max_body_size 20M;               # allowed size for a client request
    large_client_header_buffers 2 1k;       # number and size of buffers for large client headers

    # Timeouts
    client_body_timeout 30;
    client_header_timeout 30;
    keepalive_timeout 65;
    send_timeout 10;

    # Elastic Beanstalk Modification(EB_INCLUDE)

    log_format healthd '$msec"$uri"'
                       '$status"$request_time"$upstream_response_time"'
                       '$http_x_forwarded_for';

    include /etc/nginx/conf.d/*.conf;

    # End Modification
}

服务器:

upstream nodejs {
    server 127.0.0.1:8081;
    keepalive 256;
}

server {
    listen 8080;

    location / {
        proxy_pass  http://nodejs;
        #proxy_set_header   Connection "";
        proxy_http_version 1.1;
        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 Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_buffers 8 24k;
        proxy_buffer_size 2k;
    }

    gzip on;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}

【问题讨论】:

    标签: nginx socket.io sails.js amazon-elb


    【解决方案1】:

    进入 elasticbeanstack 仪表板并单击应用程序的环境。

    在左侧,有一个垂直列表,其中之一是配置,您应该单击它。向下滚动并找到负载均衡框,单击该框右上角的编辑工具以配置负载均衡器。

    在此表单中,第二个字段应标记为协议,并具有两个选项:HTTP(应选择)和 TCP。选择 TCP 并向下滚动并保存。

    还要确保在您的 ec2 AWS 管理员 > EC2 > 安全组中打开您正在使用的端口 (8081)。

    最后可能会有所帮助的是在应用的根目录中创建一个名为 .ebextensions 的目录。然后在其中创建一个文件 file.config 并将此内容添加到文件中:

    files:
        "/etc/nginx/conf.d/websocketupgrade.conf" :
            mode: "000755"
            owner: root
            group: root
            content: |
                 proxy_set_header        Upgrade         $http_upgrade;
                 proxy_set_header        Connection      "upgrade";
    

    如果所有这些都不起作用并且您不想打扰自己,只需禁用 nginx ,大多数情况下这将解决 (400) 问题。

    希望对你有帮助

    【讨论】:

    • tcp 监听器也会给出“粘性选项不适用于 TCP 协议”警告。我不确定,但这意味着负载平衡器后套接字连接会丢失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2021-02-19
    相关资源
    最近更新 更多