I have N-tier architecture web application in AWS. HTTP request flow order by this:
I want to apply WebSocket feature to this architecture. How to configure for two layer ELB and behind Nginx?
Use another port for ws:// protocol, because ELB don't allow listen same port in different mode (HTTP/TCP). For example: ws://Nginx-ELB:8081/ws-endpoint
That's split into two portions explain.
About WebSocket proxy, you can reference this configuration.
config example like this:
# 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;
}
}
Create port forwarding follow this:
Then use your AWS CLI execute:
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
Create port forwarding follow this:
DO NOT apply any load balancer policy for this ELB!
This section let me headache for several days. If apply same policy to both ELB, you never get the right result.
Now, enjoy your WebSocket with AWS ELB and Nginx.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With