Tuesday, April 24, 2018

Nginx Reverse Proxy Formulae

It should be simple but it's just not that simple, that's the reason I'm writing down some example for the proxy pass configuration here.

The reference link can be found here.

Theory:

The keyword is "If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter.....  If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified)."

Note:
1.If you specify any URI along with the server address it will replace the request URI part on the location
2. If you don't specify any URI, the full request URI on the location is appended to the target URL


Without further ado, here's some examples:
Assume nginx is setup on localhost and will redirect to www.example.com.

Configuration #1
location /some/path {
      proxy_pass http://www.example.com/link;
}
URL: http://localhost/some/path
Passed to: http://www.example.com/link
URL: http://localhost/some/path/hello
Passed to: http://www.example.com/link/hello
URL: http://localhost/some/path/
Passed to: http://www.example.com/link/


Configuration #2
location /some/path {
      proxy_pass http://www.example.com/;
}
URL: http://localhost/some/path
Passed to: http://www.example.com/
URL: http://localhost/some/path/hello
Passed to: http://www.example.com/hello
URL: http://localhost/some/path/
Passed to: http://www.example.com/


Configuration #3
location /some/path {
      proxy_pass http://www.example.com;
}
URL: http://localhost/some/path
Passed to: http://www.example.com/some/path
URL: http://localhost/some/path/hello
Passed to: http://www.example.com/some/path/hello
URL: http://localhost/some/path/
Passed to: http://www.example.com/some/path/


Configuration #4
location /some {
      proxy_pass http://www.example.com/;
}
URL: http://localhost/some
Passed to: http://www.example.com/
URL: http://localhost/some/path/hello
Passed to: http://www.example.com/path/hello
URL: http://localhost/some/
Passed to: http://www.example.com/
URL: http://localhost/some/hello?name=me
Passed to: http://www.example.com/hello?name=me