0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Difyで特定のチャットURLにBasic認証を設定する手順

Posted at

Difyで作成したチャットやワークフローは、デフォルトでは誰でもURLにアクセスできてしまいます。

そこで、特定のURLのみにBasic認証(ユーザー名とパスワードによるアクセス制限)をかけることで、外部からの不正アクセスを防止できます。

以下に、ブログ記事として使える概要(導入文)を追加し、併せてプライベート情報の警告も明記しました。記事冒頭に置くのがおすすめです。

環境

Xserver VPS

【仕様】 
vCPU 4コア 
メモリ4G
NVMe SSD 100GB

OS: AlmaLinux9.5

手順概要

手順は以下のような流れです:

  1. 必要なパッケージのインストール(apache2-utils)

  2. htpasswd ファイルの作成

  3. nginx設定ファイルへの認証ルールの追加

  4. Dockerコンテナの再起動による反映

サーバーへのSSHログインから設定の確認まで、具体的なコマンド付きで紹介しているので、再現しやすい構成になっています。

Difyのチャットを限定公開したい方や、アクセス制御を強化したい方におすすめの内容です。

🔧 手順

1. サーバーへSSHログイン

ssh root@{VPSのIPアドレス}

2. htpasswdコマンドのインストール(初回のみ)

sudo apt update
sudo apt install apache2-utils

確認コマンド:

which htpasswd
# => /usr/bin/htpasswd と表示されればOK

3. .htpasswdファイルを作成

設定ファイルのあるディレクトリに移動:

cd /root/dify/docker/nginx/conf.d/

3-1. .htpasswd存在しない場合

sudo htpasswd -c ./.htpasswd exampleuser

3-2. .htpasswd既にある場合(ユーザー追加)

sudo htpasswd ./.htpasswd exampleuser

確認:

cat .htpasswd
# => exampleuser:$apr1$.... のように表示されればOK(パスワードは暗号化)

4. nginx設定ファイルに認証ルールを追加

vi /root/dify/docker/nginx/conf.d/default.conf.template

以下のlocationブロックを追記(xxxxxは対象チャットのID等に置き換えてください):

    location /chat/xxxxx {
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
        proxy_pass http://web:3000;
        include proxy.conf;
    }

5. Dockerコンテナの再起動で反映

cd /root/dify/docker/
docker compose down
docker compose up -d

6. 設定の動作確認

docker exec -it docker-nginx-1 /bin/sh
cd /etc/nginx/conf.d
cat .htpasswd
cat default.conf

.htpasswdにユーザーが登録されているか、default.confにBasic認証設定が含まれているか確認してください。


💡参考リンク


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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?