Skip to content

Commit c52f8e6

Browse files
committed
Path rewrite [ci skip]
1 parent 75abc86 commit c52f8e6

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ The following query arguments can be used to send as a *reconfigure* request to
8080
|distribute |Whether to distribute a request to all the instances of the proxy. Used only in the *swarm* mode.|No|false|true|
8181
|pathType |The ACL derivative. Defaults to *path_beg*. See [HAProxy path](https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#7.3.6-path) for more info.|No||path_beg|
8282
|port |The internal port of a service that should be reconfigured. The port is used only in the *swarm* mode|Only in *swarm* mode|||8080|
83+
|reqRepReplace|A regular expression to apply the modification. If specified, `reqRepSearch` needs to be set as well.|No||\1\ /demo/\2|
84+
|reqRepSearch |A regular expression to search the content to be replaced. If specified, `reqRepReplace` needs to be set as well.|No||^([^\ ]\*)\ /something/(.\*)|
8385
|serviceDomain|The domain of the service. If specified, the proxy will allow access only to requests coming to that domain. Multiple domains should be separated with comma (`,`).|No||ecme.com|
8486
|serviceName |The name of the service. It must match the name of the Swarm service or the one stored in Consul.|Yes | |go-demo |
8587
|servicePath |The URL path of the service. Multiple values should be separated with comma (`,`).|Yes (unless consulTemplatePath is present)||/api/v1/books|

TODO.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
[X] Implement
66
[X] Add integration tests
7-
[ ] Test manually
7+
[X] Test manually
8+
[X] Write an article
89
[ ] Update README
910
[ ] Create a release
1011

@@ -13,6 +14,7 @@
1314
[ ] Implement
1415
[ ] Add integration tests
1516
[ ] Test manually
17+
[ ] Write an article
1618
[ ] Update README
1719
[ ] Create a release
1820

articles/swarm-mode-listener.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ curl -i $(docker-machine ip node-1)/demo/hello
286286

287287
Since Docker's networking (`routing mesh`) is performing load balancing, each of those requests is sent to a different proxy instance. Each was forwarded to the `go-demo` service endpoint, Docker networking did load balancing and resent it to one of the `go-demo` instances. As a result, all requests returned status *200 OK* proving that the combination of the proxy and the listener indeed works. All three instances of the proxy were reconfigured.
288288

289-
Let's remove the `go-demo` service before we proceed.
289+
Let's remove the `go-demo` service before we proceed with explanation of the flow.
290290

291291
```bash
292292
docker service rm go-demo
@@ -314,12 +314,79 @@ One of the important things to note is that, with a system like this, everything
314314

315315
A similar logic is used for the destination services. The proxy does not need to do load balancing. Docker networking does that for us. The only thing it needs is the name of the service and that both belong to the same network. As a result, there is no need to reconfigure the proxy every time a new release is made or when a service is scaled.
316316

317+
## Rewriting Paths
318+
319+
In some cases, you might want to rewrite the path of the incoming request before forwarding it to the destination service. We can do that using request parameters `reqRepSearch` and `reqRepReplace`.
320+
321+
As an example, we'll create the `go-demo` service that will be configured in the proxy to accept requests with the path starting with `/something`. Since the `go-demo` service allows only requests that start with `/demo`, we'll use `reqRepSearch` and `reqRepReplace` to rewrite the path.
322+
323+
The command is as follows.
324+
325+
```bash
326+
docker service create --name go-demo \
327+
-e DB=go-demo-db \
328+
--network go-demo \
329+
--network proxy \
330+
--label com.df.notify=true \
331+
--label com.df.distribute=true \
332+
--label com.df.servicePath=/something \
333+
--label com.df.port=8080 \
334+
--label com.df.reqRepSearch='^([^\ ]*)\ /something/(.*)' \
335+
--label com.df.reqRepReplace='\1\ /demo/\2' \
336+
--replicas 3 \
337+
vfarcic/go-demo
338+
```
339+
340+
Please notice that, this time, the `servicePath` is `/something`. The `reqRepSearch` specifies the regular expression that will be used to search for part of the address and the `reqRepReplace` will replace it. In this case, `/something/` will be replaced with `/demo/`. The proxy uses *PCRE compatible regular expressions*. For more information, please consult [Quick-Start: Regex Cheat Sheet](http://www.rexegg.com/regex-quickstart.html) and [PCRE Regex Cheatsheet](https://www.debuggex.com/cheatsheet/regex/pcre).
341+
342+
Please wait a few moments until the `go-demo` service is running. You can see the status of the service by executing `docker service ps go-demo`.
343+
344+
Once the `go-demo` service is up and running, we can confirm that the proxy was indeed configured correctly.
345+
346+
```bash
347+
curl -i $(docker-machine ip node-1)/something/hello
348+
```
349+
350+
The output is as follows.
351+
352+
```
353+
HTTP/1.1 200 OK
354+
Date: Sun, 11 Dec 2016 18:43:21 GMT
355+
Content-Length: 14
356+
Content-Type: text/plain; charset=utf-8
357+
358+
hello, world!
359+
```
360+
361+
We sent a request to `/something/hello`. The proxy accepted the request, rewrote the path to `/demo/hello`, and forwarded it to the `go-demo` service.
362+
363+
Let's remove the `go-demo` service before we proceed with *authentication*.
364+
365+
```bash
366+
docker service rm go-demo
367+
```
368+
317369
## Basic Authentication
318370

319371
*Docker Flow: Proxy* can provide basic authentication that can be applied on two levels. We can configure the proxy to protect all or only a selected service.
320372

321373
### Global Authentication
322374

375+
We'll start by recreating the `go-demo` service we removed earlier.
376+
377+
```bash
378+
docker service create --name go-demo \
379+
-e DB=go-demo-db \
380+
--network go-demo \
381+
--network proxy \
382+
--label com.df.notify=true \
383+
--label com.df.distribute=true \
384+
--label com.df.servicePath=/demo \
385+
--label com.df.port=8080 \
386+
--replicas 3 \
387+
vfarcic/go-demo
388+
```
389+
323390
To configure the proxy to protect all the services, we need to specify the environment variable `USERS`.
324391

325392
As an example, we'll update the `proxy` service by adding the environment variable `USERS`.

0 commit comments

Comments
 (0)