Nginx
Nginx は Nginx の Docker イメージである。
Docker Compose
オレオレ証明書を使って HTTPS 通信に対応させる。
証明書は/path/to/server.crt、/path/to/server.keyとして格納済みのものとする。
docker compose up --buildしたあとはhttps://localhostで Nginx の Welcome ページが表示される。
なおhttp://servicename:3000のリバースプロキシとして使う場合は以下のような設定になる。
コンテナ内の設定ファイルの確認方法
| $ docker compose exec -u root nginx cat /etc/nginx/conf.d/default.conf
|
Basic 認証ありのリバースプロキシ
| .
├── docker-compose.yml
└── nginx
├── .htpasswd
└── default.conf
|
1
2
3
4
5
6
7
8
9
10
11
12 | services:
nginx-basic:
image: nginx:latest
container_name: nginx-basic
restart: always
ports:
- "8080:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
- ./nginx/.htpasswd:/etc/nginx/.htpasswd:ro
extra_hosts:
- "host.docker.internal:host-gateway"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://host.docker.internal:3002/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /hoge/ {
proxy_pass http://host.docker.internal:3002/hoge/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
auth_basic "Authorization Required";
auth_basic_user_file /etc/nginx/.htpasswd;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
|
ハッシュ値を作成するためにパッケージをインストールする。
| $ sudo apt install -y apache2-utils
|
ユーザー名とパスワードのハッシュ値を設定する。
| $ sudo htpasswd -cB ./nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
|