Delivering the Client IP to Fargate with NLB + Fargate
A Target Group behind an NLB has a feature that lets it pass the originating client IP to the target without altering it.
Using this, you can implement IP-based access control.
However, with an NLB + Fargate setup, I confirmed that the IP reaching Fargate was the NLB’s private IP.
(With EC2 the client IP was passed through without any problem.)
I had the following kind of configuration in Nginx, and yet… Why??
1 | set_real_ip_from 10.10.0.0/16; |
Cause
If the target group protocol is TCP or TLS, client IP preservation is disabled by default.
It turns out that for an NLB, when the Target Group protocol is TCP or TLS, client IP preservation is disabled by default.
To add some context:
Because Fargate specifies target type = IP in the Target Group, the client IP is not preserved by default.
EC2, on the other hand, specifies an instance ID in the Target Group, and in that case client IP preservation is enabled.
Solution
As described in the AWS documentation below, enable proxy protocol v2.
Target Group configuration
1 | resource "aws_lb_target_group" "app_https" { |
Nginx configuration
Configure Nginx to accept proxy_protocol requests.
1 | server { |
Result
I successfully delivered the client IP to Fargate!
However,
when I set the Nginx error log level to debug,
I could see a message saying that proxy protocol v2 was not supported.

Looking at https://github.com/nginx/nginx/commit/9207cc84b21e94283478cee7a953b1859c4434cb, it seems to be handled without any problem.
The official docs also state that it is supported from Nginx 1.13.11 onward, so there should be no issue.
https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/
To accept the PROXY protocol v2, NGINX Plus R16 and later or NGINX Open Source 1.13.11 and later
While the log message is a bit concerning,
from the LB’s perspective the client IP is being delivered to Fargate, and Nginx appears to be interpreting it correctly.
Delivering the Client IP to Fargate with NLB + Fargate
https://kenzo0107.github.io/en/2021/04/30/send-clientip-nlb-nginx-on-fargate/
