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

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

新規登録して質問してみよう
ただいま回答率
85.48%
HTTPS

HTTPS(Hypertext Transfer Protocol Secure)はHypertext Transfer プロトコルとSSL/TLS プロトコルを組み合わせたものです。WebサーバとWebブラウザの間の通信を暗号化させて、通信経路上での盗聴や第三者によるなりすましを防止します。

nginx

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

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

1回答

3187閲覧

EC2(AL2)/Nginx環境へSSL証明書がインストールできない

_whitecat_22

総合スコア1305

HTTPS

HTTPS(Hypertext Transfer Protocol Secure)はHypertext Transfer プロトコルとSSL/TLS プロトコルを組み合わせたものです。WebサーバとWebブラウザの間の通信を暗号化させて、通信経路上での盗聴や第三者によるなりすましを防止します。

nginx

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

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

1グッド

1クリップ

投稿2020/11/25 06:23

現象

Python/DjangoでWEBシステムを作成していますが、
本番運用環境サーバへSSH接続し、NginxへのSSL証明書(certbotを利用)を導入する際に、
エラーメッセージが発生しました。

このため、HTTPS化ができず、WEBページが表示されない現象が発生しています。
これを解消し、httpsにてWEBページを表示させたいです。
原因や理由がお分かりになる方は、対処をご教示いただけませんでしょうか。

なお、ElasticIPを設定しEC2へ関連付け済み。独自ドメインも取得、DNS設定はできていまして、
HTTPS化の前までは「http://<独自ドメイン>」でWEBページが表示できていました。

ちなみに独自ドメインは、freenomで取得した<●●●●●.ml>というドメインです。

1.環境
  • Amazon EC2 (Linux2) t2.micro 64bit
  • Amazon SES
  • python 3.7.9
  • django 3.1.3
  • postgreSQL 11.5
  • Nginx 1.18.0
  • Gunicorn
2.手順

NginxへのSSL証明書の手順は、以下としました。
(参照ページ:『Amazon Linux 2 AMIになってからletsencryptがスムーズにいかない』
エラーが発生したのは、最後の「sudo certbot --nginx」の実行時です。

linux

1sudo amazon-linux-extras install nginx1 を実行 2/etc/nginxで、server_name にドメインを追記編集 3sudo systemctl enable nginx を実行 4sudo systemctl start nginx を実行 5sudo wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/ を実行 6sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm を実行 7sudo yum install certbot python2-certbot-nginx を実行 8sudo certbot --nginx を実行

発生している問題・エラーメッセージ

linux

1[<ユーザー>@ip-xxx-xxx-xxx-xxx ~]$ sudo certbot --nginx 2Saving debug log to /var/log/letsencrypt/letsencrypt.log 3Plugins selected: Authenticator nginx, Installer nginx 4Enter email address (used for urgent renewal and security notices) 5 (Enter 'c' to cancel): <連絡用メールアドレス> 6 7- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 8Please read the Terms of Service at 9https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must 10agree in order to register with the ACME server at 11https://acme-v02.api.letsencrypt.org/directory 12- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13(A)gree/(C)ancel: a 14 15- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 16Would you be willing, once your first certificate is successfully issued, to 17share your email address with the Electronic Frontier Foundation, a founding 18partner of the Let's Encrypt project and the non-profit organization that 19develops Certbot? We'd like to send you email about our work encrypting the web, 20EFF news, campaigns, and ways to support digital freedom. 21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 22(Y)es/(N)o: y 23 24No names were found in your configuration files. Please enter in your domain 25name(s) (comma and/or space separated) (Enter 'c' to cancel): whitecat-22.ml 26Obtaining a new certificate 27Performing the following challenges: 28http-01 challenge for <独自ドメイン> 29Using default addresses 80 and [::]:80 ipv6only=on for authentication. 30Waiting for verification... 31Cleaning up challenges 32Could not automatically find a matching server block for <独自ドメイン>. Set the `server_name` directive to use the Nginx installer. 33 34IMPORTANT NOTES: 35 - Unable to install the certificate 36 - Congratulations! Your certificate and chain have been saved at: 37 /etc/letsencrypt/live/<独自ドメイン>/fullchain.pem 38 Your key file has been saved at: 39 /etc/letsencrypt/live/<独自ドメイン>/privkey.pem 40 Your cert will expire on 2021-02-23. To obtain a new or tweaked 41 version of this certificate in the future, simply run certbot again 42 with the "certonly" option. To non-interactively renew *all* of 43 your certificates, run "certbot renew" 44 - Your account credentials have been saved in your Certbot 45 configuration directory at /etc/letsencrypt. You should make a 46 secure backup of this folder now. This configuration directory will 47 also contain certificates and private keys obtained by Certbot so 48 making regular backups of this folder is ideal. 49[<ユーザー>@ip-xxxx-xxx-xxx-xxx ~]$

該当のソースコード

nginx.conf の内容です。(SSL化後の設定です)

conf

1# For more information on configuration, see: 2# * Official English Documentation: http://nginx.org/en/docs/ 3# * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5user nginx; 6worker_processes auto; 7error_log /var/log/nginx/error.log; 8pid /run/nginx.pid; 9 10# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 11include /usr/share/nginx/modules/*.conf; 12 13events { 14 worker_connections 1024; 15} 16 17http { 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 65; 28 types_hash_max_size 4096; 29 30 include /etc/nginx/mime.types; 31 default_type application/octet-stream; 32 33 # Load modular configuration files from the /etc/nginx/conf.d directory. 34 # See http://nginx.org/en/docs/ngx_core_module.html#include 35 # for more information. 36 include /etc/nginx/conf.d/*.conf; 37 38 server { 39 listen 80; 40 listen [::]:80; 41 server_name <Elastic IP>; 42 return 301 https://$host/$request_uri; 43 } 44 45 # Setting for a TLS enabled server. 46 server { 47 listen 443; 48 listen [::]:443; 49 server_name <Elastic IP>; 50 root /usr/share/nginx/html; 51 52 ssl_certificate "/etc/letsencrypt/live/<独自ドメイン>/fullchain.pem"; 53 ssl_certificate_key "/etc/letsencrypt/live/<独自ドメイン>/privkey.pem"; 54 55 # Load configuration files for the default server block. 56 include /etc/nginx/default.d/*.conf; 57 58 location /static { 59 alias /usr/share/nginx/html/static; 60 } 61 62 location /media { 63 alias /usr/share/nginx/html/media; 64 } 65 66 location / { 67 proxy_set_header Host $http_host; 68 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 69 proxy_set_header X-Forwarded-proto $scheme; 70 71 proxy_pass http://127.0.0.1:8000; 72 } 73 74 location /.well-known/acme-challenge { 75 root /usr/share/nginx/html; 76 } 77 78 error_page 404 /404.html; 79 location = /40x.html { 80 } 81 82 error_page 500 502 503 504 /50x.html; 83 location = /50x.html { 84 } 85 } 86}

気にしているのは、上記ログ中の以下の箇所です。

linux

1Could not automatically find a matching server block for <独自ドメイン>. Set the `server_name` directive to use the Nginx installer. 2 3IMPORTANT NOTES: 4 - Unable to install the certificate

直訳すると、以下のようになるかと思いますが、
証明書がインストールできない原因が分からず、どのような対処を必要とするのか見えていないことです。

<独自ドメイン>に一致するサーバーブロックを自動的に見つけることができません。 Nginxインストーラーを使用するように `server_name`ディレクティブを設定します。 重要な注意事項: -証​​明書をインストールできません

試したこと

  • 背景

『動かして学ぶ Python Django 開発入門』(翔泳社:大高 隆 著)に掲載されている、日記帳サービスを書籍の通りに作成。
同書 13章 独自ドメイン化とセキュリティ対策 4節 HTTPS化を行う にて、
SSL証明書を秘密鍵の発行として、下記コマンドの実行を指示されていますが、
これは、certbot-autoの権限エラーにて上手く行かない、という情報を他の方のブログで見付けており、
上述の参考ページの手順で実行した次第です。

その他pythonのソースコードをはじめ、Linux上で行う各種設定の内容は何度も見直して、間違いは無いことを確認済みです。

これ以外には、特に試した内容はありません。

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

※上記に不足する情報があればご指摘ください。加筆もしくはコメントで補足します。

PolymetisOutis7👍を押しています

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

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

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

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

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

TaichiYanagiya

2020/11/26 07:43

server_name に IPアドレスではなく、「独自ドメイン名」を設定するといいのでは?
_whitecat_22

2020/11/26 09:04

TaichiYanagiya様 ありがとうございます。 仰る通り、nginx.conf の server_name へ<独自ドメイン名>を指定することで、エラーが解消されました。 SSL証明書の発行、および、HTTP化もでき、https://<独自ドメイン名> でWEBページを表示することができました。 書籍には、該当箇所は<Elastic IP>を、との記載であったために、それを鵜呑みにしてずっとすすめておりました。 今後は、他の方の質問・回答や、公式ドキュメントを含めて、別サイトの情報もよく調べてから質問いたします。 誠にありがとうございました。
guest

回答1

0

自己解決

TaichiYanagiya様 より:

server_name に IPアドレスではなく、「独自ドメイン名」を設定するといいのでは?

頂戴したコメントの通りです。
nginx.conf につき、server_name へ <独自ドメイン> を指定することで、エラーが解消されました。
SSL証明書の発行、および、HTTP化もでき、https://<独自ドメイン名> でWEBページを表示することができました。

conf

1# For more information on configuration, see: 2# * Official English Documentation: http://nginx.org/en/docs/ 3# * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5user nginx; 6worker_processes auto; 7error_log /var/log/nginx/error.log; 8pid /run/nginx.pid; 9 10# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 11include /usr/share/nginx/modules/*.conf; 12 13events { 14 worker_connections 1024; 15} 16 17http { 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 65; 28 types_hash_max_size 4096; 29 30 include /etc/nginx/mime.types; 31 default_type application/octet-stream; 32 33 # Load modular configuration files from the /etc/nginx/conf.d directory. 34 # See http://nginx.org/en/docs/ngx_core_module.html#include 35 # for more information. 36 include /etc/nginx/conf.d/*.conf; 37 38 server { 39 listen 80; 40 listen [::]:80; 41 server_name <独自ドメイン>; 42 return 301 https://$host/$request_uri; 43 } 44 45 # Setting for a TLS enabled server. 46 server { 47 listen 443; 48 listen [::]:443; 49 server_name <独自ドメイン>; 50 root /usr/share/nginx/html; 51 52 ssl_certificate "/etc/letsencrypt/live/<独自ドメイン>/fullchain.pem"; 53 ssl_certificate_key "/etc/letsencrypt/live/<独自ドメイン>/privkey.pem"; 54 55 # Load configuration files for the default server block. 56 include /etc/nginx/default.d/*.conf; 57 58 location /static { 59 alias /usr/share/nginx/html/static; 60 } 61 62 location /media { 63 alias /usr/share/nginx/html/media; 64 } 65 66 location / { 67 proxy_set_header Host $http_host; 68 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 69 proxy_set_header X-Forwarded-proto $scheme; 70 71 proxy_pass http://127.0.0.1:8000; 72 } 73 74 location /.well-known/acme-challenge { 75 root /usr/share/nginx/html; 76 } 77 78 error_page 404 /404.html; 79 location = /40x.html { 80 } 81 82 error_page 500 502 503 504 /50x.html; 83 location = /50x.html { 84 } 85 } 86}

投稿2020/11/26 09:18

_whitecat_22

総合スコア1305

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

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

PolymetisOutis7

2022/04/08 05:09

こちらの投稿、誠にありがとうございます。 私も同じ参考書を使っていて、色々辿って同じ症状に直面しました。本当に大変助かりました!! そもそもcertbot-autoが使えなくなってきているようです。 そこで私は次の記事を参考にしました。 https://zenn.dev/whitecat_22/articles/c353049988b034 そうすると同じ症状になったところです。 教えて頂きたいことがございまして、 What would you like to do? と聞かれたときに、 1: Attempt to reinstall this existing certificate 2: Renew & replace the certificate (may be subject to CA rate limits) とあり、1を選択しましたが良かったのでしょうか? また、 参考書のように再度nginx.confを編集しなくてもhttps化されていましたが、問題無いのでしょうか? 以上、宜しくお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問