Opened 4 years ago

Closed 4 years ago

#1067 closed defect (invalid)

behaviour of location + proxy_pass with a variable does not match behaviour with hardcoded value

Reported by: allan-simon@… Owned by:
Priority: minor Milestone:
Component: nginx-core Version: 1.9.x
Keywords: location variables Cc:
uname -a: Linux all-MacBookPro 4.2.0-42-generic #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.9.3 (Ubuntu)
built with OpenSSL 1.0.2d 9 Jul 2015
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module

Description

Here's a minimal case to reproduce

server {
  listen 80;
  location /myservice/ {
    proxy_pass http://127.0.0.1:8080/;
  }
}

with a nc -l 8080 acting as proxified proxy

the following command

curl http://localhost/myservice/foo/far

show on netcat side, as expected

GET /foo/bar HTTP/1.0
Host: 127.0.0.1:8080
Connection: close
User-Agent: curl/7.43.0
Accept: */*



now if i use a variable to store my proxified service's domain like this

server {
  listen 80;
  set $MY_SERVICE "127.0.0.1:8080";
  location /myservice/ {
    proxy_pass http://$MY_SERVICE/;
  }
}


now regardless of the URI, the proxified value will always be

GET / HTTP/1.0
Host: 127.0.0.1:8080
Connection: close
User-Agent: curl/7.43.0
Accept: */*


notice now the GET has become / , though it should be GET /foo/bar

Change History (1)

comment:1 by Maxim Dounin, 4 years ago

Resolution: invalid
Status: newclosed

Using variables in proxy_pass implies, that the URI specified is a full one. Quoting the documentation:

A server name, its port and the passed URI can also be specified using variables:

proxy_pass http://$host$uri;

This behaviour is not very intuitive, though it's how it works. Variables in proxy_pass were introduced

If you want nginx to use URI of the original request instead of the one specified in the proxy_pass, avoid specifying URI at all (note no trailing slash):

proxy_pass http://$host;

If you want to pass some changed URI, you can do so by using rewrite ... break in the same location.

Note: See TracTickets for help on using tickets.