1 |
|
2 | Routing
|
3 | =======
|
4 |
|
5 | - [Examples](#examples)
|
6 | - [Route Config Options](#route-config-options)
|
7 | - [Route Override](#route-override)
|
8 | - [Change Origin](#change-origin)
|
9 |
|
10 | When Shunter receives an incoming request it needs to know where the request should be proxied to. This is configured by setting a `routes` property when you create your shunter app, typically by requiring a config file:
|
11 |
|
12 | ```js
|
13 | var app = shunter({
|
14 | routes: require('./config/routes.json'),
|
15 | ...
|
16 | });
|
17 | ```
|
18 |
|
19 | Examples
|
20 | --------
|
21 |
|
22 | The config is used to match the incoming hostname and request url and match it to a proxy target.
|
23 |
|
24 | ```js
|
25 | {
|
26 | "www.example.com": {
|
27 | "/^\\/blog/": {
|
28 | "host": "blog.example.com",
|
29 | "port": 80
|
30 | },
|
31 | "/^\\/demo/": {
|
32 | "host": "demo.example.com"
|
33 | },
|
34 | "/^\\/about/": {
|
35 | "host": "about.example.com",
|
36 | "port": 1337
|
37 | },
|
38 | "default": {
|
39 | "host": "cms.example.com",
|
40 | "port": 8080
|
41 | }
|
42 | },
|
43 | "test-www.example.com": {
|
44 | "/^\\/blog/": {
|
45 | "host": "test-blog.example.com",
|
46 | "port": 80
|
47 | },
|
48 | "default": {
|
49 | "host": "test-cms.example.com",
|
50 | "port": 8080
|
51 | }
|
52 | },
|
53 | "localhost": {
|
54 | "default": {
|
55 | "host": "127.0.0.1",
|
56 | "port": 5000
|
57 | }
|
58 | }
|
59 | }
|
60 | ```
|
61 |
|
62 | The table shows how different requests would be mapped using this config:
|
63 |
|
64 | | Host Header | Request Url | Proxy Destination |
|
65 | | :--------------------- | :-------------------- | :--------------------------------------------- |
|
66 | | `www.example.com` | `/blog/article123` | `blog.example.com:80/blog/article123` |
|
67 | | `www.example.com` | `/demo/test` | `demo.example.com/demo/test` |
|
68 | | `www.example.com` | `/about/contact.html` | `about.example.com:1337/about/contact.html` |
|
69 | | `www.example.com` | `/foo` | `cms.example.com:8080/foo` |
|
70 | | `test-www.example.com` | `/blog/article123` | `test-blog.example.com:80/blog/article123` |
|
71 | | `test-www.example.com` | `/about/contact.html` | `test-cms.example.com:8080/about/contact.html` |
|
72 | | `test-www.example.com` | `/foo` | `test-cms.example.com:8080/foo` |
|
73 | | `foo.example.com` | `/foo/bar` | `127.0.0.1:5000/foo/bar` |
|
74 |
|
75 |
|
76 | Route Config Options
|
77 | --------------------
|
78 |
|
79 | By default if none of the regex patterns are matched Shunter will use the route under the `default` key. The name of the default key can be configured by providing the `route-config` option when you start your shunter app. So if you had the config:
|
80 |
|
81 | ```js
|
82 | {
|
83 | "www.example.com": {
|
84 | "custom": {
|
85 | "host": "127.0.0.1",
|
86 | "port": 1337
|
87 | },
|
88 | "default": {
|
89 | "host": "127.0.0.1",
|
90 | "port": 5000
|
91 | }
|
92 | }
|
93 | }
|
94 | ```
|
95 |
|
96 | And ran your Shunter app with `--route-config=custom` requests would be routed to port 1337 instead of 5000.
|
97 |
|
98 |
|
99 | Route Override
|
100 | -------------
|
101 |
|
102 | Routing can be overridden entirely by setting the `route-override` option. Running your Shunter app with `--route-override=http://www.example.com:1337` would route all requests to that destination. You could also route to an IPv4: `--route-override=8.8.8.8:80`
|
103 |
|
104 | **n.b**: If you do not specify a protocol (`http` or `https`) when setting a route via `route-override` then it will default to `http`. Please also note that `https` is currently unsupported by Shunter.
|
105 | ```
|
106 | --route-override=$BACKEND_URL
|
107 | ```
|
108 | | BACKEND_URL | Proxy Destination |
|
109 | | :---------------------------- | :-------------------------- |
|
110 | | `http://www.example.com` | `http://www.example.com` |
|
111 | | `https://foo.example.com` | `https://foo.example.com` |
|
112 | | `www.example.com` | `http://www.example.com` |
|
113 | | `www.example.com:80` | `http://www.example.com:80` |
|
114 | | `localhost` | `http://localhost` |
|
115 | | `127.0.0.1:5000` | `http://127.0.0.1:5000` |
|
116 | | `8.8.8.8` | `http://8.8.8.8` |
|
117 | | `https://8.8.8.8:80` | `https://8.8.8.8:80` |
|
118 |
|
119 |
|
120 | You can use the `--origin-override` (`-g`) option in conjunction with the `route-override` option to set `changeOrigin: true` for the overriding route. See [Change Origin](#change-origin) for more details.
|
121 |
|
122 | This would be useful when deploying your Shunter app to a platform that makes use of environment variables. For example you could start up a Shunter app with:
|
123 | ```
|
124 | node app -p $PORT --route-override=$BACKEND_APP --origin-override
|
125 | ```
|
126 |
|
127 |
|
128 | Change Origin
|
129 | -------------
|
130 |
|
131 | In addition to setting the host and port for the proxy target there is an additional `changeOrigin` option that can be used. When this is set to true (it defaults to false) Shunter will update the host header to match the destination server. So with the following config:
|
132 |
|
133 | ```js
|
134 | {
|
135 | "www.example.com": {
|
136 | "default": {
|
137 | "host": "cms.example.com",
|
138 | "port": 8080,
|
139 | "changeOrigin": true
|
140 | }
|
141 | }
|
142 | }
|
143 | ```
|
144 |
|
145 | The application on cms.example.com would receive requests with a host header of `cms.example.com` instead of `www.example.com`. The original host header will be passed through to the backend in an `X-Orig-Host` header.
|
146 |
|
147 | ---
|
148 |
|
149 | Related:
|
150 |
|
151 | - [Full API Documentation](index.md)
|