mirror of https://github.com/docker/docs.git
Redirects info
This commit is contained in:
parent
81c062ab69
commit
6b2900d324
|
|
@ -5,12 +5,11 @@ description: Learn how to implement redirects using swarm services and the
|
||||||
keywords: routing, proxy, redirects
|
keywords: routing, proxy, redirects
|
||||||
---
|
---
|
||||||
|
|
||||||
Once the [layer 7 routing solution is enabled](../deploy/index.md), you can
|
# Implementing redirects
|
||||||
start using it in your swarm services. In this example we'll deploy a simple
|
The following example deploys a simple
|
||||||
service that can be reached at `app.example.org`. We'll also redirect
|
service that can be reached at `app.example.org`. Requests to `old.example.org` are redirected to that service.
|
||||||
requests to `old.example.org` to that service.
|
|
||||||
|
|
||||||
To do that, create a docker-compose.yml file with:
|
Create a docker-compose.yml file as shown in the following example:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.2"
|
version: "3.2"
|
||||||
|
|
@ -35,7 +34,7 @@ networks:
|
||||||
|
|
||||||
Note that the demo service has labels to signal that traffic for both
|
Note that the demo service has labels to signal that traffic for both
|
||||||
`app.example.org` and `old.example.org` should be routed to this service.
|
`app.example.org` and `old.example.org` should be routed to this service.
|
||||||
There's also a label indicating that all traffic directed to `old.example.org`
|
There is also a label indicating that all traffic directed to `old.example.org`
|
||||||
should be redirected to `app.example.org`.
|
should be redirected to `app.example.org`.
|
||||||
|
|
||||||
Set up your CLI client with a [UCP client bundle](../../user-access/cli.md),
|
Set up your CLI client with a [UCP client bundle](../../user-access/cli.md),
|
||||||
|
|
@ -45,13 +44,13 @@ and deploy the service:
|
||||||
docker stack deploy --compose-file docker-compose.yml demo
|
docker stack deploy --compose-file docker-compose.yml demo
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also use the CLI to test if the redirect is working, by running:
|
You can also use the CLI to test if the redirect is working, by running the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl --head --header "Host: old.example.org" http://<ucp-ip>:<http-port>
|
curl --head --header "Host: old.example.org" http://<ucp-ip>:<http-port>
|
||||||
```
|
```
|
||||||
|
|
||||||
You should see something like:
|
You should see something like the following output:
|
||||||
|
|
||||||
```none
|
```none
|
||||||
HTTP/1.1 302 Moved Temporarily
|
HTTP/1.1 302 Moved Temporarily
|
||||||
|
|
@ -63,7 +62,68 @@ Connection: keep-alive
|
||||||
Location: http://app.example.org/
|
Location: http://app.example.org/
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also test that the redirect works from your browser. For that, you
|
You can also test that the redirect works from your browser. For that, make sure you add entries for both `app.example.org` and
|
||||||
need to make sure you add entries for both `app.example.org` and
|
`old.example.org` to your `/etc/hosts` file and map them to the IP address
|
||||||
`old.example.org` to your `/etc/hosts` file, mapping them to the IP address
|
|
||||||
of a UCP node.
|
of a UCP node.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
------------------------SHOULD THE FOLLOWING BE INCLUDED AS WELL? ------------------------------------------
|
||||||
|
|
||||||
|
The following example publishes a service and configures a redirect from `old.local` to `new.local`.
|
||||||
|
|
||||||
|
First, create an overlay network so that service traffic is isolated and secure:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$> docker network create -d overlay demo
|
||||||
|
1se1glh749q1i4pw0kf26mfx5
|
||||||
|
```
|
||||||
|
|
||||||
|
Next, create the service with the redirect:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$> docker service create \
|
||||||
|
--name demo \
|
||||||
|
--network demo \
|
||||||
|
--detach=false \
|
||||||
|
--label com.docker.lb.hosts=old.local,new.local \
|
||||||
|
--label com.docker.lb.port=8080 \
|
||||||
|
--label com.docker.lb.redirects=http://old.local,http://new.local \
|
||||||
|
--env METADATA="demo-new" \
|
||||||
|
ehazlett/docker-demo
|
||||||
|
```
|
||||||
|
|
||||||
|
Interlock detects when the service is available and publishes it. After tasks are running
|
||||||
|
and the proxy service is updated, the application is available via `http://new.local`
|
||||||
|
with a redirect configured that sends `http://old.local` to `http://new.local`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$> curl -vs -H "Host: old.local" http://127.0.0.1
|
||||||
|
* Rebuilt URL to: http://127.0.0.1/
|
||||||
|
* Trying 127.0.0.1...
|
||||||
|
* TCP_NODELAY set
|
||||||
|
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
|
||||||
|
> GET / HTTP/1.1
|
||||||
|
> Host: old.local
|
||||||
|
> User-Agent: curl/7.54.0
|
||||||
|
> Accept: */*
|
||||||
|
>
|
||||||
|
< HTTP/1.1 302 Moved Temporarily
|
||||||
|
< Server: nginx/1.13.6
|
||||||
|
< Date: Wed, 08 Nov 2017 19:06:27 GMT
|
||||||
|
< Content-Type: text/html
|
||||||
|
< Content-Length: 161
|
||||||
|
< Connection: keep-alive
|
||||||
|
< Location: http://new.local/
|
||||||
|
< x-request-id: c4128318413b589cafb6d9ff8b2aef17
|
||||||
|
< x-proxy-id: 48854cd435a4
|
||||||
|
< x-server-info: interlock/2.0.0-development (147ff2b1) linux/amd64
|
||||||
|
<
|
||||||
|
<html>
|
||||||
|
<head><title>302 Found</title></head>
|
||||||
|
<body bgcolor="white">
|
||||||
|
<center><h1>302 Found</h1></center>
|
||||||
|
<hr><center>nginx/1.13.6</center>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue