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 .
Exampleconfig:
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;
}
}
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 \
aws elb set-load-balancer-policies-for-backend-server \
AP-ELB
Create port forwarding:
- 80 (HTTP) forwarding to 80 (HTTP)
- 8081 (TCP) 80 (TCP)
ELB!
. ELB, .
WebSocket AWS ELB Nginx.