Tasks for Ingress-Nginx
TOC
PrerequisitesMax ConnectionsRequest TimeoutSession Affinity (Sticky Sessions)Header ModificationURL RewriteHSTS (HTTP Strict Transport Security)Rate LimitingWAFForward-header controlHTTPSTLS re-encrypt and verify backend certificateTLS edge terminationPassthroughDefault CertificateAdd Pod Annotation in IngressNginxPreserve Source IPVia HAProxy Proxy ProtocolHow it worksHow to configureVia MetalLB with externalTrafficPolicy=LocalHow it worksHow to configurePrerequisites
Max Connections
Request Timeout
Session Affinity (Sticky Sessions)
Header Modification
URL Rewrite
HSTS (HTTP Strict Transport Security)
Rate Limiting
WAF
Forward-header control
HTTPS
TLS re-encrypt and verify backend certificate
verify backend https certificate
TLS edge termination
Passthrough
Default Certificate
use the following yaml to deploy an ingress-nginx with default certificate
please refer to default-ssl-certificate
Add Pod Annotation in IngressNginx
Preserve Source IP
When traffic passes through load balancers or proxies, the original client IP address can be lost due to NAT (Network Address Translation). Preserving the source IP is important for:
- Access control and security policies
- Accurate logging and analytics
- Rate limiting per client
- Geolocation-based routing
Via HAProxy Proxy Protocol
How it works
The PROXY protocol is a network protocol for preserving client connection information when proxying TCP connections. It works by prepending a header to the TCP connection that contains the original source IP and port.
Traffic flow:
- Client connects to HAProxy load balancer
- HAProxy prepends PROXY protocol header with original client IP to the connection
- Ingress-Nginx receives the connection and parses the PROXY protocol header
- Ingress-Nginx extracts the real client IP from the header
- Backend applications receive the correct client IP in
X-Forwarded-ForandX-Real-IPheaders
Advantages:
- Works with any load balancer that supports PROXY protocol (HAProxy, AWS NLB, etc.)
- Preserves source IP across multiple proxy layers
- No impact on routing or node selection
Considerations:
- Both the load balancer and Ingress-Nginx must be configured to use PROXY protocol
- All traffic to Ingress-Nginx must use PROXY protocol once enabled (mixing PROXY and non-PROXY traffic will cause connection failures)
How to configure
Configure your HAProxy load balancer to send PROXY protocol headers, then deploy an ingress-nginx with proxy-protocol support enabled:
For more details, see PROXY protocol documentation.
Note: HAProxy can use TCP mode to forward traffic without handling TLS certificates. Since the PROXY protocol works at the TCP layer, you can let Ingress-Nginx handle HTTPS termination and certificate management directly, eliminating the need to configure certificates in HAProxy.
Via MetalLB with externalTrafficPolicy=Local
How it works
When using a Kubernetes Service with type: LoadBalancer, the default behavior (externalTrafficPolicy: Cluster) performs source NAT, which replaces the client IP with the node's IP. Setting externalTrafficPolicy: Local preserves the source IP by:
- Direct routing: Traffic is only routed to pods on the same node that received the traffic
- No SNAT: The kube-proxy does not perform source NAT, preserving the original client IP
- Health checks: Only nodes with healthy local pods are included in the load balancer pool
Traffic flow:
- Client connects to MetalLB virtual IP
- MetalLB routes traffic directly to a node with Ingress-Nginx pods
- Traffic goes directly to the local Ingress-Nginx pod without SNAT
- Ingress-Nginx sees the real client IP
- Backend applications receive the correct client IP in headers
Advantages:
- Simple configuration, no additional protocol required
- Native Kubernetes feature
- Lower latency (no extra proxy hop)
Considerations:
- Uneven load distribution: Traffic can only go to nodes with local pods, potentially causing imbalanced load
- Pod scheduling: Ingress-Nginx pods must be scheduled on nodes that MetalLB can route to (use nodeSelector to ensure alignment)
- Health check behavior: If all local pods are unhealthy, the node is removed from load balancing entirely
How to configure
Deploy an ingress-nginx with externalTrafficPolicy: Local and ensure pod placement aligns with MetalLB configuration:
Important: The nodeSelector must match the nodes in your MetalLB address pool configuration to ensure Ingress-Nginx pods are scheduled on nodes that can receive traffic from MetalLB.
For more details, see externalTrafficPolicy documentation.