|
| 1 | +As a request goes through proxies (such as load balancers) the host, port, and |
| 2 | +scheme may change, and that makes it a challenge to create links that point to the correct |
| 3 | +host, port, and scheme from a client perspective. |
| 4 | + |
| 5 | +https://tools.ietf.org/html/rfc7239[RFC 7239] defines the `Forwarded` HTTP header |
| 6 | +that proxies can use to provide information about the original request. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +[[forwarded-headers-non-standard]] |
| 11 | +=== Non-standard Headers |
| 12 | + |
| 13 | +There are other non-standard headers, too, including `X-Forwarded-Host`, `X-Forwarded-Port`, |
| 14 | +`X-Forwarded-Proto`, `X-Forwarded-Ssl`, and `X-Forwarded-Prefix`. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +[[x-forwarded-host]] |
| 19 | +==== X-Forwarded-Host |
| 20 | + |
| 21 | +While not standard, https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host[`X-Forwarded-Host: <host>`] |
| 22 | +is a de-facto standard header that is used to communicate the original host to a |
| 23 | +downstream server. For example, if a request of `https://example.com/resource` is sent to |
| 24 | +a proxy which forwards the request to `http://localhost:8080/resource`, then a header of |
| 25 | +`X-Forwarded-Host: example.com` can be sent to inform the server that the original host was `example.com`. |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +[[x-forwarded-port]] |
| 30 | +==== X-Forwarded-Port |
| 31 | + |
| 32 | +While not standard, `X-Forwarded-Port: <port>` is a de-facto standard header that is used to |
| 33 | +communicate the original port to a downstream server. For example, if a request of |
| 34 | +`https://example.com/resource` is sent to a proxy which forwards the request to |
| 35 | +`http://localhost:8080/resource`, then a header of `X-Forwarded-Port: 443` can be sent |
| 36 | +to inform the server that the original port was `443`. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +[[x-forwarded-proto]] |
| 41 | +==== X-Forwarded-Proto |
| 42 | + |
| 43 | +While not standard, https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto[`X-Forwarded-Proto: (https|http)`] |
| 44 | +is a de-facto standard header that is used to communicate the original protocol (e.g. https / https) |
| 45 | +to a downstream server. For example, if a request of `https://example.com/resource` is sent to |
| 46 | +a proxy which forwards the request to `http://localhost:8080/resource`, then a header of |
| 47 | +`X-Forwarded-Proto: https` can be sent to inform the server that the original protocol was `https`. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +[[x-forwarded-ssl]] |
| 52 | +==== X-Forwarded-Ssl |
| 53 | + |
| 54 | +While not standard, `X-Forwarded-Ssl: (on|off)` is a de-facto standard header that is used to communicate the |
| 55 | +original protocol (e.g. https / https) to a downstream server. For example, if a request of |
| 56 | +`https://example.com/resource` is sent to a proxy which forwards the request to |
| 57 | +`http://localhost:8080/resource`, then a header of `X-Forwarded-Ssl: on` to inform the server that the |
| 58 | +original protocol was `https`. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +[[x-forwarded-prefix]] |
| 63 | +==== X-Forwarded-Prefix |
| 64 | + |
| 65 | +While not standard, https://microsoft.github.io/reverse-proxy/articles/transforms.html#defaults[`X-Forwarded-Prefix: <prefix>`] |
| 66 | +is a de-facto standard header that is used to communicate the original URL path prefix to a |
| 67 | +downstream server. |
| 68 | + |
| 69 | +The definition of the path prefix is most easily defined by an example. For example, consider |
| 70 | +the following proxy to server mapping of: |
| 71 | + |
| 72 | +[subs="-attributes"] |
| 73 | +---- |
| 74 | +https://example.com/api/{path} -> http://localhost:8080/app1/{path} |
| 75 | +---- |
| 76 | + |
| 77 | +The prefix is defined as the porition of the URL path before the capture group of `+{path}+`. |
| 78 | +For the proxy, the prefix is `/api` and for the server the prefix is `/app1`. In this case, |
| 79 | +the header of `X-Forwarded-Prefix: /api` can be sent to indicate the original prefix of `/api` |
| 80 | +which overrides the server's prefix of `/app1`. |
| 81 | + |
| 82 | +The `X-Forwarded-Prefix` is flexible because it overrides the existing prefix. This means that |
| 83 | +the server prefix can be replaced (as demonstrated above), removed, or modified. |
| 84 | + |
| 85 | +The previous example demonstrated how to replace the prefix, but at times users may want to |
| 86 | +instruct the server to remove the prefix. For example, consider the proxy to server |
| 87 | +mapping of: |
| 88 | + |
| 89 | +[subs="-attributes"] |
| 90 | +---- |
| 91 | +https://app1.example.com/{path} -> http://localhost:8080/app1/{path} |
| 92 | +https://app2.example.com/{path} -> http://localhost:8080/app2/{path} |
| 93 | +---- |
| 94 | + |
| 95 | +In the `app1` example above, the proxy has an empty prefix and the server has a prefix of |
| 96 | +`/app1`. The header of ``X-Forwarded-Prefix: `` can be sent to indicate the original empty |
| 97 | +prefix which overrides the server's prefix of `/app1`. In the `app2` example above, the proxy |
| 98 | +has an empty prefix and the server has a prefix of `/app2`. The header of ``X-Forwarded-Prefix: `` |
| 99 | +can be sent to indicate the original empty prefix which overrides the server's prefix of `/app2`. |
| 100 | + |
| 101 | +[NOTE] |
| 102 | +==== |
| 103 | +A common usecase is that an organization pays licenses per production application server. |
| 104 | +This means that they prefer to deploy multiple applications to each application server to |
| 105 | +avoid paying the licensing fees. |
| 106 | +
|
| 107 | +Another common usecase is that organizations may be using more resource intensive |
| 108 | +application servers. This means that they prefer to deploy multiple applications to each |
| 109 | +application server to avoid consuming additional resources. |
| 110 | +
|
| 111 | +In both of these usecases, applications must define a non-empty context root because there is |
| 112 | +more than one application associated to the same application server. |
| 113 | +
|
| 114 | +While their application is deployed with a non-empty context root, they do not want this |
| 115 | +expressed in the path of their URLs because they use a different subdomain for each application. |
| 116 | +Using different subdomains for each application provides benefits such as: |
| 117 | +
|
| 118 | +* Added security (e.g. same origin policy) |
| 119 | +* Allows for scaling the applications differently (a different domain can point to different |
| 120 | +IP addresses) |
| 121 | +
|
| 122 | +The example above illustrates how to implement such a scenario. |
| 123 | +==== |
| 124 | + |
| 125 | +In some cases, a proxy may want to insert a prefix in front of the existing prefix. For |
| 126 | +example, consider the proxy to server mapping of: |
| 127 | + |
| 128 | +[subs="-attributes"] |
| 129 | +---- |
| 130 | +https://example.com/api/app1/{path} -> http://localhost:8080/app1/{path} |
| 131 | +---- |
| 132 | + |
| 133 | +In the example above, the proxy has a prefix of `/api/app1` and the server has a prefix of |
| 134 | +`/app1`. The header of `X-Forwarded-Prefix: /api/app1` can be sent to indicate the original |
| 135 | +prefix of `/api/app1` which overrides the server's prefix of `/app1`. |
0 commit comments