Opened 9 years ago

Last modified 9 years ago

#944 new enhancement

Enchance $server_addr to return original IP even after local DNAT

Reported by: kvrico@… Owned by:
Priority: major Milestone:
Component: nginx-core Version: 1.9.x
Keywords: dnat server ip Cc:
uname -a:
nginx -V: nginx version: nginx/1.9.13
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=njs-91543c86f412/nginx --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --with-ld-opt=-Wl,-E

Description

I'd like to suggest to improve current $server_addr or introduce new veriable, that will return origin IP address used by a client to connect to nginx, even if connection was diverted by local DNAT rule.

My usecase is following:

a) I create iptables rules on the host:

# iptables -t nat -A OUTPUT -p tcp -d 192.168.170.1 --dport 7654 -j DNAT --to-destination 127.0.0.1:11123
# iptables -t nat -A OUTPUT -p tcp -d 192.168.170.2 --dport 7654 -j DNAT --to-destination 127.0.0.1:11123

b) Run nginx on localhost port 11123
c) Use telnet to hit 192.168.170.1:7654 and 192.168.170.2:7654
d) I need load balancer to choose different upstreams depending on the
address I specified on step (c)

Please note, that HAProxy supports this usecase via "dst" acl
https://github.com/haproxy/haproxy/blob/master/src/proto_tcp.c#L600

Thanks.

Change History (1)

comment:1 by Maxim Dounin, 9 years ago

Do you have any problems with using different listening sockets to distinguish requests to different destinations? It looks like a most obvious solution to the exact use case you've described.

Note: See TracTickets for help on using tickets.