Skip to content

Commit 85e6d30

Browse files
committed
Improve variable configuration for source IP address
1 parent 0aa41ec commit 85e6d30

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

controllers/nginx/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ The following table shows the options, the default value and a description.
462462
|ignore-invalid-headers|"true"|
463463
|keep-alive|"75"|
464464
|log-format-stream|[$time_local] $protocol $status $bytes_sent $bytes_received $session_time|
465-
|log-format-upstream|[$the_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status|
465+
|log-format-upstream|[$the_real_ip] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status|
466466
|map-hash-bucket-size|"64"|
467467
|max-worker-connections|"16384"|
468468
|proxy-body-size|same as body-size|

controllers/nginx/pkg/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const (
4848

4949
gzipTypes = "application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component"
5050

51-
logFormatUpstream = `%v - [$the_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status`
51+
logFormatUpstream = `%v - [$the_real_ip] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status`
5252

5353
logFormatStream = `[$time_local] $protocol $status $bytes_sent $bytes_received $session_time`
5454

@@ -365,7 +365,7 @@ func NewDefault() Configuration {
365365
// is enabled.
366366
func (cfg Configuration) BuildLogFormatUpstream() string {
367367
if cfg.LogFormatUpstream == logFormatUpstream {
368-
return fmt.Sprintf(cfg.LogFormatUpstream, "$the_x_forwarded_for")
368+
return fmt.Sprintf(cfg.LogFormatUpstream, "$the_real_ip")
369369
}
370370

371371
return cfg.LogFormatUpstream

controllers/nginx/pkg/config/config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func TestBuildLogFormatUpstream(t *testing.T) {
2828
curLogFormat string
2929
expected string
3030
}{
31-
{true, logFormatUpstream, fmt.Sprintf(logFormatUpstream, "$the_x_forwarded_for")},
32-
{false, logFormatUpstream, fmt.Sprintf(logFormatUpstream, "$the_x_forwarded_for")},
31+
{true, logFormatUpstream, fmt.Sprintf(logFormatUpstream, "$the_real_ip")},
32+
{false, logFormatUpstream, fmt.Sprintf(logFormatUpstream, "$the_real_ip")},
3333
{true, "my-log-format", "my-log-format"},
3434
{false, "john-log-format", "john-log-format"},
3535
}

controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl

+11-9
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,17 @@ http {
135135
'' $server_port;
136136
}
137137

138-
map $pass_access_scheme $the_x_forwarded_for {
139-
default $remote_addr;
140-
https $proxy_protocol_addr;
138+
{{ if $cfg.UseProxyProtocol }}
139+
map $http_x_forwarded_for $the_real_ip {
140+
default $http_x_forwarded_for;
141+
'' $proxy_protocol_addr;
141142
}
142-
143-
map $pass_access_scheme $the_real_ip {
144-
default $remote_addr;
145-
https $proxy_protocol_addr;
143+
{{ else }}
144+
map $http_x_forwarded_for $the_real_ip {
145+
default $http_x_forwarded_for;
146+
'' $remote_addr;
146147
}
148+
{{ end }}
147149

148150
# map port 442 to 443 for header X-Forwarded-Port
149151
map $pass_server_port $pass_port {
@@ -394,7 +396,7 @@ http {
394396
proxy_set_header Connection $connection_upgrade;
395397

396398
proxy_set_header X-Real-IP $the_real_ip;
397-
proxy_set_header X-Forwarded-For $the_x_forwarded_for;
399+
proxy_set_header X-Forwarded-For $the_real_ip;
398400
proxy_set_header X-Forwarded-Host $best_http_host;
399401
proxy_set_header X-Forwarded-Port $pass_port;
400402
proxy_set_header X-Forwarded-Proto $pass_access_scheme;
@@ -564,7 +566,7 @@ stream {
564566

565567
server {
566568
listen {{ $udpServer.Port }} udp;
567-
{{ if $IsIPV6Enabled }}listen [::]:{{ $udpServer.Port }} udp;{{ end }}
569+
{{ if $IsIPV6Enabled }}listen [::]:{{ $udpServer.Port }} udp;{{ end }}
568570
proxy_responses 1;
569571
proxy_pass udp-{{ $udpServer.Backend.Namespace }}-{{ $udpServer.Backend.Name }}-{{ $udpServer.Backend.Port }};
570572
}

0 commit comments

Comments
 (0)