0
votes

Application is behind a load balancer on aws(elasticbeanstack). Before load balancing it had worked. The application responds 400 Bad Request. There are many questions about this topic. I tried a lot. Sticky sessions, change upstream proxy to http://localhost, connection upgrade... but i couldn't figure out. Here is nginx error.log:

*1217 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.31.90, server: , request: "GET /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", upstream: "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 configuarions;

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
}

server:

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

1 Answers

1
votes

Go into the elasticbeanstack dashboard and click on the environment for your app.

On the left, there is a vertical list and one of them is Configuration which you should click on. scroll down and find the Load Balancing box, click the edit tool in the upper-right corner of that box to configure the load balancer.

In this form, the second field should be labeled Protocol and have two options: HTTP (which should be selected) and TCP. select TCP and scroll down and save.

also make sure to open the port that you are using (8081) at your ec2 AWS Admin > EC2 > Security Groups.

Last thing that maybe will help is to create a directory with the name .ebextensions in the root of your app. then create a file in it file.config and add this content to the file :

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";

If all of that doesn't work and you don't wanna bother yourself , just disable nginx , most of the time this will solve the (400) issue.

Hope it helps