質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

uWSGI

uWSGIは、PythonでWebサービスを動かすアプリケーションサーバの一つです。WSGI(Web Server Gateway Interface)アプリケーションコンテナの一種で、WSGIに則ったDjangoやFlaskなどで動かすことができます。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

1回答

1885閲覧

docker-compose を使用した nginx サイトの SSL 化について(NGINX / MySQL / uWSGI / Django)

rokutimpo

総合スコア12

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

uWSGI

uWSGIは、PythonでWebサービスを動かすアプリケーションサーバの一つです。WSGI(Web Server Gateway Interface)アプリケーションコンテナの一種で、WSGIに則ったDjangoやFlaskなどで動かすことができます。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2022/05/06 13:09

編集2022/05/08 01:24

【 前提 】

docker-compose を使用したウェブサイトを作成しています。
SSL 対応をしようとしていますが、https での接続ができず困っています。

諸事情でコンテナを使用して構成する必要があります。
ホストOS側で確認したところ、どうやら443 番ポートは listen 状態にはなっているようです。

以下のような構成で、 SSL 化を行う方法をご存じの方がいらっしゃいましたら
ご教示頂けますと幸いです。

Let's Encrypt の証明書はホスト側の OS で cron 等を用いて定期的に更新し、NGINX を再起動することで対応しようと考え、現在以下の構成となっています。

< ディレクトリ構成 >

├── django
│   ├── Dockerfile
│   └── requirements.txt
├── docker-compose.yml
├── mysql
│   └── .env
├── nginx
│   ├── certs
│   │   └──
│   ├── nginx.conf
│   └── uwsgi_params
├── sql
│   └── init.sql
├── src
│   └── django のプロジェクトディレクトリ
└── static(静的ファイルを集めています)

./nginx/certs ディレクトリに certbot で取得した証明書をコピーしています。

< サーバーの構成 >

  • 物理サーバ: Amazon lightsail
  • WEBサーバ: NGINX ( docker イメージ nginx:latest) → 2022 年 5 月 6 日時点で 1.21.6 を使用しています。
  • アプリケーション・サーバ: uWSGI ( docker イメージ python:3.9 からインストールしたもの)
  • フレームワーク: Django ( docker イメージ python:3.9 からインストールしたもの)
  • データベース: MySQL ( docker イメージ mysql:8.0)

実現したいこと

サーバ証明書に Let's Encrypt を使用して SSL 対応を行いたい。

発生している問題(ホスト OS 側のシェルにて確認)

以下 6 点のコマンド入力を行い、確認作業を行いました。
全てホスト OS 側のシェルで実行しています。

bash

1# input 1 2curl -i http://ドメイン名前 3 4# output 1 5HTTP/1.1 200 OK 6Server: nginx 7Date: Fri, 06 May 2022 11:35:16 GMT 8Content-Type: text/html; charset=utf-8 9Content-Length: 3744 10Connection: keep-alive 11X-Frame-Options: DENY 12Vary: Cookie 13X-Content-Type-Options: nosniff 14Referrer-Policy: same-origin 15Cross-Origin-Opener-Policy: same-origin

↑ html の内容もきちんと返って来ます。

bash

1# input 2 2curl -i https://ドメイン名 3 4# output 2 5curl: (28) Failed to connect to ドメイン名 port 443: Connection timed out

↑ タイムアウトになってしまいます。

bash

1# input 3 2sudo lsof -i -P 3 4# output 3 (dockerの出力部分以外、長いのでカットしています。) 5~~~~~~ 6docker-pr 74639 root 4u IPv4 538863 0t0 TCP *:3306 (LISTEN) 7docker-pr 74644 root 4u IPv6 538872 0t0 TCP *:3306 (LISTEN) 8docker-pr 74858 root 4u IPv4 539960 0t0 TCP *:443 (LISTEN) 9docker-pr 74862 root 4u IPv6 539967 0t0 TCP *:443 (LISTEN) 10docker-pr 74875 root 4u IPv4 540029 0t0 TCP *:80 (LISTEN) 11docker-pr 74880 root 4u IPv6 540036 0t0 TCP *:80 (LISTEN) 12~~~~~~

↑ 443 ポートは listen になっています。

bash

1# input 4 2sudo docker logs NGINXのコンテナ名 3 4# output 4 5/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration 6/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ 7/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 810-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 910-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf 10/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh 11/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh 12/docker-entrypoint.sh: Configuration complete; ready for start up 13curl を送ったIPアドレス - - [06/May/2022:11:35:16 +0000] "GET / HTTP/1.1" 200 3744 "-" "curl/7.68.0" "-"

なお、この時点で http://ドメイン名 宛に GET リクエストを送信したものはログとして出力されますが、
https://ドメイン名 宛にリクエストを送ったものについてはログの出力がありません。

bash

1# 実行時の日付は日本時間で 2022 年 05 月 06 日です。 2# input 5 3 sudo openssl x509 -in /etc/letsencrypt/live/ドメイン名/fullchain.pem -noout -dates 4 5# output 5 6notBefore=May 6 04:57:17 2022 GMT 7notAfter=Aug 4 04:57:16 2022 GMT

有効期限は残っています。

コピー後のディレクトリの中身、およびコンテナ内のマウントしたディレクトリも確認しました。
しかし、権限、所有者の情報が変わった様子もありません。

bash

1# docker-compose.yml で django コンテナの /tmp/mylog.log にログ出力するようにしています。 2 3# input 6 4sudo docker-compose exec django cat /tmp/mylog.log 5 6# output 6 7*** Starting uWSGI 2.0.20 (64bit) on [Fri May 6 11:34:57 2022] *** 8compiled with version: 10.2.1 20210110 on 06 May 2022 08:42:41 9os: Linux-5.4.0-1018-aws #18-Ubuntu SMP Wed Jun 24 01:15:00 UTC 2020 10nodename: adbb29352c07 11machine: x86_64 12clock source: unix 13pcre jit disabled 14detected number of CPU cores: 1 15current working directory: /code 16detected binary path: /usr/local/bin/uwsgi 17uWSGI running as root, you can use --uid/--gid/--chroot options 18 19~~~途中略~~~ 20 21*** uWSGI is running in multiple interpreter mode *** 22spawned uWSGI master process (pid: 1) 23spawned uWSGI worker 1 (pid: 7, cores: 1) 24Python auto-reloader enabled 25[pid: 7|app: 0|req: 1/1] IPアドレス () {32 vars in 349 bytes} [Fri May 6 20:35:16 2022] GET / => generated 3744 bytes in 360 msecs (HTTP/1.1 200) 7 headers in 222 bytes (1 switches on core 0) 26~~~

httpのログ出力は見つかりました。

該当のソースコード

以下3点を記載します。

①./docker-compose.yml
②./nginx/nginx.conf
③./nginx/uwsgi_params

①./docker-compose.yml

docker

1# ./docker-compose.yml 2 3version: '3' 4 5services: 6 nginx: 7 image: nginx:latest 8 ports: 9 - '80:80' 10 - '443:443' 11 12 volumes: 13 - ./nginx/nginx.conf:/etc/nginx/nginx.conf 14 - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params 15 - ./static:/static 16 # ---------------------------------------------------------------- 17 # 事前にホスト OS の /etc/letsencrypt/live/ドメイン名 のディレクトリを 18 # docker-compose を行うディレクトリ(./enginx/certs)にコピーしています。 19 # 20 # そして、ホスト OS の nginx/certs ディレクトリを nginx コンテナの 21 # /etc/nginx/certs にマウントしています。 22 # ---------------------------------------------------------------- 23 - ./nginx/certs:/etc/nginx/certs 24 depends_on: 25 - django 26 27 mysql: 28 image: mysql:8.0 29 command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --innodb-buffer-pool-size=16M 30 ports: 31 - "3306:3306" 32 env_file: 33 - ./mysql/.env 34 volumes: 35 - ./mysql:/var/lib/mysql 36 - ./sql:/docker-entrypoint-initdb.d 37 38 django: 39 build: ./django 40 command: uwsgi --socket :8001 --module mysite.wsgi --py-autoreload 1 --logto /tmp/mylog.log 41 volumes: 42 - ./src:/code 43 - ./static:/static 44 expose: 45 - "8001" 46 env_file: 47 - ./django/.env 48 depends_on: 49 - mysql 50

②./nginx/nginx.conf

nginx.conf

1# ./nginx/nginx.conf 2 3user nginx; 4worker_processes 1; 5error_log /var/log/nginx/error.log warn; 6pid /var/run/nginx.pid; 7 8events { 9 worker_connections 1024; 10} 11 12http { 13 include /etc/nginx/mime.types; 14 default_type application/octet-stream; 15 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 16 '$status $body_bytes_sent "$http_referer" ' 17 '"$http_user_agent" "$http_x_forwarded_for"'; 18 access_log /var/log/nginx/access.log main; 19 sendfile on; 20 keepalive_timeout 65; 21 22 upstream django_8001 { 23 ip_hash; 24 server django:8001; 25 } 26 27 server { 28 listen 80; 29 listen 443 ssl; 30 ssl_certificate /etc/nginx/certs/fullchain.pem; 31 ssl_certificate_key /etc/nginx/certs/privkey.pem; 32 server_name ドメイン名; 33 charset utf-8; 34 35 location /static/ { 36 autoindex on; 37 alias /static/; 38 } 39 40 location / { 41 uwsgi_pass django_8001; 42 include /etc/nginx/uwsgi_params; 43 } 44 } 45 server_tokens off; 46}

③./nginx/uwsgi_params

uwsgi_params

1# ./nginx/uwsgi_params 2 3uwsgi_param QUERY_STRING $query_string; 4uwsgi_param REQUEST_METHOD $request_method; 5uwsgi_param CONTENT_TYPE $content_type; 6uwsgi_param CONTENT_LENGTH $content_length; 7 8uwsgi_param REQUEST_URI $request_uri; 9uwsgi_param PATH_INFO $document_uri; 10uwsgi_param DOCUMENT_ROOT $document_root; 11uwsgi_param SERVER_PROTOCOL $server_protocol; 12uwsgi_param REQUEST_SCHEME $scheme; 13uwsgi_param HTTPS $https if_not_empty; 14 15uwsgi_param REMOTE_ADDR $remote_addr; 16uwsgi_param REMOTE_PORT $remote_port; 17uwsgi_param SERVER_PORT $server_port; 18uwsgi_param SERVER_NAME $server_name;

補足情報(FW/ツールのバージョンなど)

  • 物理サーバ: Amazon Lightsail (Ubuntu 20.04)
  • WEBサーバ: NGINX ( docker イメージ nginx:latest) → 2022 年 5 月 6 日時点で 1.21.6 を使用しています。
  • アプリケーション・サーバ: uWSGI ( docker イメージ python:3.9 からインストールしたもの)
  • フレームワーク: Django ( docker イメージ python:3.9 からインストールしたもの)
  • データベース: MySQL ( docker イメージ mysql:8.0)

さいごに

拙い質問文となってしまい恐縮ですが、どなたか知恵をお貸し頂けますと幸甚です。

構成自体を変えた方がいい、このコンテナ使った方がいい or やめた方がいい 等
どんな意見でもご教示頂けると、とても嬉しいです。

どうぞ宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bsdfan

2022/05/07 03:49

firewallで443は開いてますか?
rokutimpo

2022/05/07 07:46

bsdfan さま お返事いただきありがとうございます。 はい、ホスト側で ufw の設定は完了しており、ファイアウォールも起動した状態です。
rokutimpo

2022/05/07 16:12

bsdfan さま すみません、私の確認不足でした。 Amazon Lightsail 側の設定で 443 ポートが閉じていましたので、開けたところ無事公開することが出来ました。 完全に盲点でした。 気づかせてくださり本当にありがとうございました ><
guest

回答1

0

自己解決

bsdfan さまからの一言により解決しました。

Amazon Lightsail の場合、Lightsail のウェブページ(management console) から設定を行う必要がありました。
https://lightsail.aws.amazon.com/ls/webapp/home/instances

デフォルトでは IPv4 のファイアウォールで以下のように設定されています。

アプリケーションプロトコルポートまたは範囲/コード制限
SSHTCP22任意の IPv4 アドレス, Lightsail ブラウザ SSH/RDP
HTTPTCP80任意の IPv4 アドレス
----------------

ここに 『+ ルールを追加』ボタンから以下を追加する必要がありました。

アプリケーションプロトコルポートまたは範囲/コード制限
HTTPSTCP443任意の IPv4 アドレス

上記の作業で、SSL 化を行うことが出来るようになりました。
ファイアウォールについては以下の記事が参考になりました。

https://lightsail.aws.amazon.com/ls/docs/ja_jp/articles/understanding-firewall-and-port-mappings-in-amazon-lightsail

以上です。

投稿2022/05/07 16:24

rokutimpo

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問