はじめに
以下の記事をほぼほぼ参考に試してみた備忘録です。
https://qiita.com/zawawahoge/items/d58ab6b746625e8d4457
ディレクトリ構成
$ tree nginx_to_sort/
nginx_to_sort/
├── cat-server
│ └── index.html
├── docker-compose.yml
├── dog-server
│ └── index.html
└── reverse-proxy
└── nginx.conf
docker-compose.ymlを準備
$ cat docker-compose.yml
version: '3'
services:
dog-server:
image: nginx
container_name: 'dog-container'
volumes:
- ./dog-server:/usr/share/nginx/html
ports:
- 7000:80
cat-server:
image: nginx
container_name: 'cat-container'
volumes:
- ./cat-server:/usr/share/nginx/html
ports:
- 7001:80
reverse-proxy:
image: nginx
volumes:
- ./reverse-proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
nginx.confを準備
$ cat reverse-proxy/nginx.conf
events {
worker_connections 16;
}
http {
server {
listen 80;
server_name localhost;
location /dog {
proxy_pass http://host.docker.internal:7000/;
proxy_redirect off;
}
location /cat {
proxy_pass http://host.docker.internal:7001/;
proxy_redirect off;
}
}
}
dogサーバー・catサーバーにhtmlファイル配置
$ cat dog-server/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>犬好きのためのページ</title>
</head>
<body>
<h1>犬好きのためのページ</h1>
</body>
</html>
$ cat cat-server/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>猫好きのためのページ</title>
</head>
<body>
<h1>猫好きのためのページ</h1>
</body>
</html>
docker-compose実行
$ docker-compose up -d
Creating network "nginx_reverse_proxy_as_loadbarancer_default" with the default driver
Creating nginx_reverse_proxy_as_loadbarancer_reverse-proxy_1 ... done
Creating cat-container ... done
Creating dog-container ... done
動作検証
$ curl --verbose localhost/dog -p 80
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET /dog HTTP/1.1
> Host: localhost
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.19.10
< Date: Wed, 12 May 2021 15:21:26 GMT
< Content-Type: text/html
< Content-Length: 264
< Connection: keep-alive
< Last-Modified: Sat, 08 May 2021 05:38:52 GMT
< ETag: "609623ec-108"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>犬好きのためのページ</title>
</head>
<body>
<h1>犬好きのためのページ</h1>
</body>
* Connection #0 to host localhost left intact
</html>* Trying 0.0.0.80...
* TCP_NODELAY set
* Immediate connect fail for 0.0.0.80: No route to host
* Closing connection 1
curl: (7) Couldn't connect to server
* Closing connection 0
$ curl --verbose localhost/cat -p 80
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET /cat HTTP/1.1
> Host: localhost
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.19.10
< Date: Wed, 12 May 2021 15:22:20 GMT
< Content-Type: text/html
< Content-Length: 264
< Connection: keep-alive
< Last-Modified: Sat, 08 May 2021 05:39:57 GMT
< ETag: "6096242d-108"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>猫好きのためのページ</title>
</head>
<body>
<h1>猫好きのためのページ</h1>
</body>
* Connection #0 to host localhost left intact
</html>* Trying 0.0.0.80...
* TCP_NODELAY set
* Immediate connect fail for 0.0.0.80: No route to host
* Closing connection 1
curl: (7) Couldn't connect to server
* Closing connection 0