Skip to content

Commit f3669ea

Browse files
shaun-nx“shaun-nx”haywoodshpre-commit-ci[bot]
authored
Set value of $remote_addr to client IP when TLSPassthrough and Proxy Protocol are enabled (#3341)
* Add set-real-ip-from in server block in stream context * set real ip on when proxy-protocol is enabled * Update README for proxy-protocol example * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove blank space Co-authored-by: “shaun-nx” <“s.odonovan@f5.com”> Co-authored-by: Haywood Shannon <5781935+haywoodsh@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 585a84e commit f3669ea

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

examples/shared-examples/proxy-protocol/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# PROXY Protocol
22

33
Proxies and load balancers, such as HAProxy or ELB, can pass the client's information (the IP address and the port) to the next proxy or load balancer via the PROXY Protocol. To enable NGINX Ingress Controller to receive that information, use the `proxy-protocol` ConfigMaps configuration key as well as the `real-ip-header` and the `set-real-ip-from` keys. Once you enable the PROXY Protocol, it is enabled for every Ingress and VirtualServer resource.
4+
**NOTE** TransportServer resource supports PROXY Protocol only when TLS Passthrough is enabled for the Ingress Controller.
45

56
## Syntax
67

@@ -11,11 +12,15 @@ proxy-protocol: "True | False"
1112

1213
Additionally, you must configure the following keys:
1314
* **real-ip-header**: Set its value to `proxy_protocol`.
14-
* **set-real-ip-from**: Set its value to the IP address or the subnet of the proxy or the load balancer. See https://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
15+
* **set-real-ip-from**: Set its value to the IP address or the subnet of the proxy or the load balancer. See [set-real-ip-from](https://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from)
1516

1617
## Example
1718

18-
In the example below we configure the PROXY Protocol via a ConfigMaps resource. The IP address of the proxy which is in front of the Ingress Controller is `192.168.192.168`.
19+
In the example below we configure the PROXY Protocol via a ConfigMaps resource. `set-real-ip-from` is set to `192.168.0.0/16`. This is the CIDR range of the proxy that sits in front of the Ingress Controller in this example. You can set this to `0.0.0.0/0` to trust all IPs.
20+
After we create the ConfigMaps resource, the client's IP address is available via the `$remote_addr` variable in the NGINX configuration.
21+
By default, NGINX Ingress Controller logs the value of this variable and also passes the value to the backend service in the `X-Real-IP` header.
22+
23+
The default log format for NGINX is `'$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'`
1924

2025
```yaml
2126
kind: ConfigMap
@@ -25,6 +30,5 @@ metadata:
2530
data:
2631
proxy-protocol: "True"
2732
real-ip-header: "proxy_protocol"
28-
set-real-ip-from: "192.168.192.168"
33+
set-real-ip-from: "192.168.0.0/16"
2934
```
30-
After we create the ConfigMaps resource, in the NGINX configuration the client's IP address is available via the `$remote_addr` variable. By default, NGINX Ingress Controller logs the value of this variable and also passes the value to the backend service in the `X-Real-IP` header.

internal/configs/version1/nginx-plus.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ stream {
308308
listen 443{{if .ProxyProtocol}} proxy_protocol{{end}};
309309
{{if not .DisableIPV6}}listen [::]:443{{if .ProxyProtocol}} proxy_protocol{{end}};{{end}}
310310

311+
{{if .ProxyProtocol}}
312+
{{range $setRealIPFrom := .SetRealIPFrom}}
313+
set_real_ip_from {{$setRealIPFrom}};{{end}}
314+
{{end}}
315+
311316
ssl_preread on;
312317

313318
proxy_protocol on;

internal/configs/version1/nginx.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ stream {
242242
listen 443{{if .ProxyProtocol}} proxy_protocol{{end}};
243243
{{if not .DisableIPV6}}listen [::]:443{{if .ProxyProtocol}} proxy_protocol{{end}};{{end}}
244244

245+
246+
{{if .ProxyProtocol}}
247+
{{range $setRealIPFrom := .SetRealIPFrom}}
248+
set_real_ip_from {{$setRealIPFrom}};{{end}}
249+
{{end}}
250+
245251
ssl_preread on;
246252

247253
proxy_protocol on;

0 commit comments

Comments
 (0)