LoginSignup
0
0

More than 1 year has passed since last update.

traefikにBasic認証を設定

Posted at

上記の設定が出来ていることが前提となります。

■Basic認証用のID、パスワードを生成

# ID: test
# PW: test
htpasswd -nb test test

■ID、パスワードを直接記載する場合のdocker-compose.yml

version: '3.7'

services:
  test-app-test:
    .
    .
    .
+    labels:
+      - traefik.http.routers.test-app-test.middlewares=auth
+      # パスワードは test です
+      - traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$eGwwMz30$$..Nko1IUf72kLRzLgS1cA.
  • traefik.http.routers.test-app-test.middlewares に設定した authtraefik.http.middlewares.【ここにauthが入ります】.basicauth.users に記載をします。
  • basicauth.users の値は複数の設定が可能で「,(カンマ)」区切りで設定を行います。
  • パスワードは「htpasswd」コマンドで作成します
  • ymlファイルで$を使用する場合、$$に変更する必要があります

■認証ファイルにID、パスワードを指する場合のdocker-compose.yml

version: '3.7'

services:
  test-app-test:
    .
    .
    .
    volumes:
    .
    .
    .
+      - /tmp/.htpassword:/tmp/.htpassword
+    labels:
+      - traefik.http.routers.test-app-test.middlewares=auth
+      - traefik.http.middlewares.auth.basicauth.usersfile=/tmp/.htpassword
/tmp/.htpassword
test:$apr1$eGwwMz30$..Nko1IUf72kLRzLgS1cA.
  • volumes に /tmp/.htpassword を追加していますが、コンテナ内にファイルを直接作成しても問題はありません
  • ファイルを指定する場合、basicauth.usersではなくbasicauth.usersfileになります

■関連記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0