1
- As a request goes through proxies ( such as load balancers) the host, port, and
1
+ As a request goes through proxies such as load balancers the host, port, and
2
2
scheme may change, and that makes it a challenge to create links that point to the correct
3
3
host, port, and scheme from a client perspective.
4
4
@@ -14,7 +14,6 @@ There are other non-standard headers, too, including `X-Forwarded-Host`, `X-Forw
14
14
`X-Forwarded-Proto`, `X-Forwarded-Ssl`, and `X-Forwarded-Prefix`.
15
15
16
16
17
-
18
17
[[x-forwarded-host]]
19
18
==== X-Forwarded-Host
20
19
@@ -25,7 +24,6 @@ a proxy which forwards the request to `http://localhost:8080/resource`, then a h
25
24
`X-Forwarded-Host: example.com` can be sent to inform the server that the original host was `example.com`.
26
25
27
26
28
-
29
27
[[x-forwarded-port]]
30
28
==== X-Forwarded-Port
31
29
@@ -36,7 +34,6 @@ communicate the original port to a downstream server. For example, if a request
36
34
to inform the server that the original port was `443`.
37
35
38
36
39
-
40
37
[[x-forwarded-proto]]
41
38
==== X-Forwarded-Proto
42
39
@@ -47,7 +44,6 @@ a proxy which forwards the request to `http://localhost:8080/resource`, then a h
47
44
`X-Forwarded-Proto: https` can be sent to inform the server that the original protocol was `https`.
48
45
49
46
50
-
51
47
[[x-forwarded-ssl]]
52
48
==== X-Forwarded-Ssl
53
49
@@ -58,78 +54,69 @@ original protocol (e.g. https / https) to a downstream server. For example, if a
58
54
original protocol was `https`.
59
55
60
56
61
-
62
57
[[x-forwarded-prefix]]
63
58
==== X-Forwarded-Prefix
64
59
65
60
While not standard, https://microsoft.github.io/reverse-proxy/articles/transforms.html#defaults[`X-Forwarded-Prefix: <prefix>`]
66
61
is a de-facto standard header that is used to communicate the original URL path prefix to a
67
62
downstream server.
68
63
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:
64
+ Use of `X-Forwarded-Prefix` can vary by deployment scenario, and needs to be flexible to
65
+ allow replacing, removing, or prepending the path prefix of the target server.
66
+
67
+ _Scenario 1: Override path prefix_
71
68
72
69
[subs="-attributes"]
73
70
----
74
71
https://example.com/api/{path} -> http://localhost:8080/app1/{path}
75
72
----
76
73
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`.
74
+ The prefix is the start of the path before the capture group `+{path}+`. For the proxy,
75
+ the prefix is `/api` while for the server the prefix is `/app1`. In this case, the proxy
76
+ can send `X-Forwarded-Prefix: /api` to have the original prefix `/api` override the
77
+ server prefix `/app1`.
81
78
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.
79
+ _Scenario 2: Remove path prefix_
84
80
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:
81
+ At times, an application may want to have the prefix removed. For example, consider the
82
+ following proxy to server mapping:
88
83
89
84
[subs="-attributes"]
90
85
----
91
86
https://app1.example.com/{path} -> http://localhost:8080/app1/{path}
92
87
https://app2.example.com/{path} -> http://localhost:8080/app2/{path}
93
88
----
94
89
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`.
90
+ The proxy has no prefix, while applications `app1` and `app2` have path prefixes
91
+ `/app1` and `/app2` respectively. The proxy can send ``X-Forwarded-Prefix: `` to
92
+ have the empty prefix override server prefixes `/app1` and `/app2`.
100
93
101
94
[NOTE]
102
95
====
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.
96
+ A common case for this deployment scenario is where licenses are paid per
97
+ production application server, and it is preferable to deploy multiple applications per
98
+ server to reduce fees. Another case is I/O bound applications, and the need to deploy
99
+ multiple applications to each application server to consume less resources.
100
+
101
+ In this scenario, applications need a non-empty context root because there are multiple
102
+ applications on the same server. However, this should not be visible in URL paths of
103
+ the public API where applications may use different subdomains that provides benefits
104
+ such as:
105
+
106
+ * Added security, e.g. same origin policy
107
+ * Independent scaling of applications (different domain points to different IP address)
123
108
====
124
109
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:
110
+ _Scenario 3: Insert path prefix_
111
+
112
+ In other cases, it may be necessary to prepend a prefix. For example, consider the
113
+ following proxy to server mapping:
127
114
128
115
[subs="-attributes"]
129
116
----
130
117
https://example.com/api/app1/{path} -> http://localhost:8080/app1/{path}
131
118
----
132
119
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`.
120
+ In this case , the proxy has a prefix of `/api/app1` and the server has a prefix of
121
+ `/app1`. The proxy can send `X-Forwarded-Prefix: /api/app1` to have the original prefix
122
+ `/api/app1` override the server prefix `/app1`.
0 commit comments