How to configure AWS ELB and Nginx for the WebSocket protocol?

I have an N-tier architecture web application in AWS. HTTP request stream:

  • Nginx-ELB (public ELB, proxy server for Nginx )
  • Nginx (EC2 instances on a public subnet, listening on port 80, AP-ELB proxy )
  • AP-ELB (internal ELB, proxy server for AP-Server )
  • AP-Server (EC2 instances on the private subnet, listening on port 80)

I want to apply the WebSocket function to this architecture. How to configure for a two-layer ELB and for Nginx?

+4
source share
1 answer

Use a different port for the ws: // protocol, because ELB does not allow listening on the same port in a different mode (HTTP / TCP). For example: ws: // Nginx-ELB : 8081 / ws-endpoint

This division into two explains.

Nginx Section

  • listen on port 80 for HTTP, then the proxy to the AP-ELB port .
  • listen on port 8081 for WebSocket, then the proxy server for AP-ELB port 8081.

About WebSocket proxy you can refer to this configuration .

Example

config:

# Web
server {
  listen       80;
  server_name  localhost;

  charset utf-8;

  error_log /var/log/nginx/lnmnt/error.log error;
  access_log off;

  set $upstream_endpoint      <ap_elb_domain_name>;

  more_set_headers  'Cache-Control: max-age=0, no-cache, no-store';
  location / {
    proxy_connect_timeout       75;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    proxy_set_header        Host $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Host    $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_pass              $upstream_adm_endpoint;
  }
}

# WebSocket
server {
  listen  8081 proxy_protocol;
  server_name localhost;
  error_log /var/log/nginx/lnmnt/websocket.error.log error;
  access_log off;
  real_ip_header proxy_protocol;

  set $upstream_ws_endpoint   <ap_elb_domain_name>:8081;

  location / {
    proxy_set_header        Host $host;
    proxy_http_version      1.1;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection "upgrade";
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_pass              $upstream_ws_endpoint;
  }
}

ELB Section

Nginx-elb

Create port forwarding:

  • 80 (HTTP) forwarding to 80 (HTTP)
  • 8081 (TCP) Forwarding to 8081 (TCP)

Then use AWS CLI execution:

aws elb create-load-balancer-policy \
  --load-balancer-name Nginx-ELB \
  --policy-name EnableProxyProtocol \
  --policy-type-name ProxyProtocolPolicyType \
  --policy-attributes AttributeName=ProxyProtocol,AttributeValue=True

aws elb set-load-balancer-policies-for-backend-server \
  --load-balancer-name Nginx-ELB \
  --instance-port 8081 \
  --policy-names EnableProxyProtocol

AP-ELB

Create port forwarding:

  • 80 (HTTP) forwarding to 80 (HTTP)
  • 8081 (TCP) 80 (TCP)

ELB!

. ELB, .

WebSocket AWS ELB Nginx.

+6

Source: https://habr.com/ru/post/1613811/


All Articles