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

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

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

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

nginx

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

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

2回答

1576閲覧

Rails + AWS + nginxでエラー(nginx: [warn] duplicate value "TLSv1.2" in /etc/nginx/conf.d/webapp.conf)

begenner

総合スコア79

HTTPS

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

nginx

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

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2019/10/15 14:39

編集2019/10/17 06:27

現在AWS(amazon linux2)にRailsアプリをデプロイし、nginxの設定をしています。

ElasticIPを叩いてRailsアプリが動作しているのが確認できたので、
下記の2記事1セットで AWS + Rails + HTTPS に対応していそうな

  1. [Rails][Nginx][AWS] Let's EncryptをEC2上のRailsに入れてHttpsにする - Qiita
  2. Amazon Linux2とLet's EncryptでSSL対応サーバを0から爆速構築 - Qiita

を参考にしました。
しかし上記タイトルのエラーが発生したため、他のサイトを探しましたが、該当するような記事が見つからなかったため、
nginxの設定ファイルをどのように編集すればいいか全くわかりません(m_ m)
もしわかる方がいらっしゃればご教授いただきますようよろしくお願いいたします(m
_m)

*結論(解決策)

上記の2 で実行、反映された状態で
/etc/nginx/conf.d/webapp.conf 内に
include /etc/letsencrypt/options-ssl-nginx.conf;
という行があり(追加され)、

/etc/nginx/conf.d/webapp.confssl_protocols TLSv1 TLSv1.1 TLSv1.2;

/etc/letsencrypt/options-ssl-nginx.confssl_protocols TLSv1.2;
で重複が発生していたため warningが発生していました。

/etc/nginx/conf.d/webapp.confssl_protocols TLSv1 TLSv1.1 TLSv1.2;
の行をコメントアウト(もしくは)削除することでwarningは発生しなくなりました。
経緯は回答の方にあります。

yu_1985さん、本当にありがとうございました!!

動作環境

Rails

  • Ruby: 2.4.5
  • Rails: 4.2.11.1
  • nginx: 1.16.1

AWS

  • Amazon linux2
  • EC2、RDS(MySQL)
  • Route53設定済

ドメイン

  • freenom(Route53と紐付け済)

証明書

  • Let'sEncrypt

期待する動作

nginxのエラーがなくなり、AWS上のRailsアプリが登録済ドメインでHTTPS規格で動作すること

エラー内容

  • 下記の設定ファイルを保存しsudo nginx -tを実行した時[warn] duplicate value "TLSv1.2"と警告が出る

(ファイルに同じ文字列は存在しない)

nginxのシンタックスチェック、再起動後のステータス確認結果

bash

1[username@ip-xxx-xxx-xxx-xxx ~]$ sudo nginx -t 2# nginx: [warn] duplicate value "TLSv1.2" in /etc/nginx/conf.d/webapp.conf:66 3# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 4# nginx: configuration file /etc/nginx/nginx.conf test is successful 5 6 7[username@ip-xxx-xxx-xxx-xxx ~]$ sudo service nginx restart 8# Redirecting to /bin/systemctl restart nginx.service 9 10 11[username@ip-xxx-xxx-xxx-xxx ~]$ sudo service nginx status 12# Redirecting to /bin/systemctl status nginx.service 13# ● nginx.service - The nginx HTTP and reverse proxy server 14# Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) 15# Active: active (running) since 火 2019-10-15 23:08:09 JST; 4s ago 16# Process: 11316 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) 17# Process: 11313 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) 18# Process: 11311 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) 19# Main PID: 11319 (nginx) 20# CGroup: /system.slice/nginx.service 21# ├─11319 nginx: master process /usr/sbin/nginx 22# └─11320 nginx: worker process 23# 24# 10月 15 23:08:08 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server... 25# 10月 15 23:08:08 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal nginx[11313]: nginx: [warn] duplicate value "TLSv1.2" in /etc/nginx/conf.d/webapp.conf:66 26# 10月 15 23:08:08 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal nginx[11313]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 27# 10月 15 23:08:08 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal nginx[11313]: nginx: configuration file /etc/nginx/nginx.conf test is successful 28# 10月 15 23:08:09 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal nginx[11316]: nginx: [warn] duplicate value "TLSv1.2" in /etc/nginx/conf.d/webapp.conf:66 29# 10月 15 23:08:09 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.

nginxの設定ファイル1(/etc/nginx/nginx.conf)

bash

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 2048; 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 default_server; 40 listen [::]:80 default_server; 41 server_name _; 42 root /usr/share/nginx/html; 43 44 # Load configuration files for the default server block. 45 include /etc/nginx/default.d/*.conf; 46 47 location / { 48 } 49 50 error_page 404 /404.html; 51 location = /40x.html { 52 } 53 54 error_page 500 502 503 504 /50x.html; 55 location = /50x.html { 56 } 57 } 58 59# Settings for a TLS enabled server. 60# 61# server { 62# listen 443 ssl http2 default_server; 63# listen [::]:443 ssl http2 default_server; 64# server_name _; 65# root /usr/share/nginx/html; 66# 67# ssl_certificate "/etc/pki/nginx/server.crt"; 68# ssl_certificate_key "/etc/pki/nginx/private/server.key"; 69# ssl_session_cache shared:SSL:1m; 70# ssl_session_timeout 10m; 71# ssl_ciphers HIGH:!aNULL:!MD5; 72# ssl_prefer_server_ciphers on; 73# 74# # Load configuration files for the default server block. 75# include /etc/nginx/default.d/*.conf; 76# 77# location / { 78# } 79# 80# error_page 404 /404.html; 81# location = /40x.html { 82# } 83# 84# error_page 500 502 503 504 /50x.html; 85# location = /50x.html { 86# } 87# }

nginxの設定ファイル2(/etc/nginx/conf.d/webapp.conf)

bash

1```bash 2# /etc/nginx/conf.d/webapp.conf 3 4# log directory 5error_log /var/www/rails/webapp/log/nginx.error.log; 6access_log /var/www/rails/webapp/log/nginx.access.log; 7 8# max body size 9#client_max_body_size 2G; 10 11upstream app_server { 12 # for UNIX domain socket setups 13 server unix:/var/www/rails/webapp/tmp/sockets/.unicorn.sock fail_timeout=0; 14} 15 16server { 17 listen 443 ssl; 18 19 server_name domainName; 20 21 # 接続制限の設定(nginx so increasing this is generally safe..) 22 # 接続を保つ秒数 23 keepalive_timeout 5; 24 25 # クライアントからのリクエストボディは2Gまで許容 26 client_max_body_size 2G; 27 28 # path for static files 29 root /var/www/rails/webapp/public; 30 31 # page cache loading 32 try_files $uri/index.html $uri.html $uri @app; 33 34 location @app { 35 proxy_set_header X-Real-IP $remote_addr; 36 37 proxy_set_header X-Forwarded-Proto $scheme; 38 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 39 proxy_set_header Host $http_host; 40 } 41 42 # Railsエラーページ 43 error_page 500 502 503 504 /500.html; 44 45 location = /500.html { 46 root /var/www/rails/webapp/public; 47 } 48 49 50 51 52 # listen 443 ssl; # managed by Certbot 53 ssl_certificate /etc/letsencrypt/live/domainName/fullchain.pem; # managed by Certbot 54 ssl_certificate_key /etc/letsencrypt/live/domainName/privkey.pem; # managed by Certbot 55 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot 56 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot 57 58 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 59} 60 61server { 62 listen 80; 63 server_name domainName; 64 return 301 https://$host$request_uri; 65}

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

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

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

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

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

yu_1985

2019/10/15 15:22

nginxの他のconfと設定が重複してないですか?
begenner

2019/10/15 22:43

バックアップ用のファイルがあったので(webapp.conf.bak)それを削除してみましたが、上記の確認コマンドで得られる結果は同じでした。 nginxのファイル名は webapp.conf でいつも作成していたため、 sudo find / -name webapp.conf で検索をかけましたが表示されたのは /etc/nginx/conf.d/webapp.conf の1ファイルだけだったのでnginx用の他のconfは現在特にないと思います。 下記のサイトも参考にしたので確認しましたが、ファイルの場所名前は同じなので記述しませんでした。 https://qiita.com/yoshi111kazu/items/e8b29aa2b103f6e4ec03
begenner

2019/10/15 23:30

すいません、間違っていました。 今 sudo find / -name *.conf でかけたところ 検索結果の中にファイル、ディレクトリ名にnginxが含まれるファイルが56あり、そのうち関係しそうなのが もっとも関連しそうなファイル /etc/nginx/nginx.conf /etc/nginx/nginx.conf.default 5ファイル(不明) /usr/share/nginx/modules/* 外部証明書発行のために作成されたと思われるもの (13ファイル)/opt/eff.org/certbot/venv/lib/python2.7/site-packages/certbot_nginx/* (4ファイル)/usr/lib/python2.7/site-packages/certbot_nginx/tls_configs/* Railsアプリ(gem unicorn)内 (4件)/var/www/rails/webapp/vendor/bundle/ruby/2.4.0/gems/unicorn-5.4.1/* というファイルがありました。 /etc/nginx/nginx.conf あたりが影響ありそうですが削除した方がいいのでしょうか?
yu_1985

2019/10/16 02:12

nginx.confはnginxのベースになる設定ファイルなので消したら設定を読み込めなくなります。 nginx.confの中でconf.d配下の*.confをIncludeしているところがあるはずです。 nginx.conf.defaultはnginx.confのデフォルト設定を書いたファイルなので消しても影響はないですが、バックアップのためにそのままにしといたほうがよいかなと。 nginx.confの内容を確認するのとconf.d配下に他にファイルはないか確認してみましょう。
yu_1985

2019/10/16 03:09

追記いただいたようですが、/etc/nginxの中よりも、nginx.confでincludeしているディレクトリのほうが重要です。 include /etc/nginx/conf.d/*.conf; include /etc/nginx/default.d/*.conf; としている箇所がありますよね?
begenner

2019/10/16 03:15 編集

/etc/nginx/conf.d 配下には 上記の webapp.conf /etc/nginx/default.d 配下には ファイルは特に存在していない状況です(m_ _m)
guest

回答2

0

ベストアンサー

nginx.confの方で include /etc/nginx/conf.d/*.conf;としている箇所がありますが、そこでwebapp.confを読み込んでいます。
webapp.confでは下の方でhttpsへリダイレクトする設定を入れるために80番ポートをリッスンしていますがが、nginx.confでも80番ポートをリッスンしている箇所があり、そこがバッティングしているように見えます。
どちらかにまとめる必要があるかと思います。

エラーの内容が403なのでそこが直接の原因かはわかりませんが…。

今回の問題に対する答えとはずれてしまいますが、個人的にはせっかくAWSを使っているならサーバ側にはSSLの設定を入れずにEC2インスタンスをELB配下に置き、ELBにACMで発行した証明書を設定してSSL化することをおすすめしたいです。
ELBは現在3種類ありますが、ALBであればhttpsへのリダイレクトはコンソール上だけで設定が可能です。

投稿2019/10/16 03:50

yu_1985

総合スコア7427

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

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

begenner

2019/10/16 13:25

ご回答いただきありがとうございます。 今回はあくまでELBを利用せずにやってみたいと思っています(エラーがなくなり、Railsアプリが問題なく動作できたのが確認できたらおっしゃっている構成でやってみようと思います)。 現状としては回答にあった 2ファイルで一方をコメントアウトして再起動してみましたが sudo nginx -t で表示される実行結果は上記と同じで、どちらもブラウザ上で 403 Forbidden が発生しています。 nginx: [warn] duplicate value "TLSv1.2" をググってみると https://community.letsencrypt.org/t/certbot-certificates/52769 といった同じような感じのquestionが投稿されています。 nginx: [warn] duplicate value "TLSv1.2" in /etc/nginx/conf.d/webapp.conf:66 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful の3行の nginx: [warn] duplicate value "TLSv1.2" という部分が気になるのですが、これに該当する部分を探すには全く別のアプローチが必要になるのでしょうか?
yu_1985

2019/10/16 15:26

ああ、include /etc/letsencrypt/options-ssl-nginx.conf という箇所がありますね。 それはどうなってますか? dupulicate valueなので、設定が重複している箇所があることが想定されます。 また、forbiddenの理由がこれかはわかりません。 /var/www/rails/webapp/public/の権限はどうなってますか?
begenner

2019/10/16 22:12

/etc/letsencrypt/options-ssl-nginx.conf に ssl_protocols TLSv1.2 という行があったので、試しに /etc/nginx/conf.d/webapp.conf のssl_protocols の行をコメントアウトすると nginx: [warn] duplicate value "TLSv1.2" の警告がなくなりました!!
yu_1985

2019/10/17 02:44

その状態でアクセスしてもforbiddenになりますか? また、httpsにしないでhttpで同じURLアクセスした場合はどうでしょうか。
begenner

2019/10/17 02:59

画像を追加しましたが、 Welcome to nginx on Fedora! といった表示がされています
yu_1985

2019/10/17 03:10

httpをhttpsにリダイレクトさせない状態でhttpsのページにアクセスするとどうなりますか? httpのほうの設定が root /usr/share/nginx/html; になっているのでhttpのほうは何もしないとデフォルトのnginxのページが表示されるのは正しい挙動です。
begenner

2019/10/17 03:20

/etc/nginx/nginx.conf のserver ブロックの中を全てコメントアウトしてしまえばいいでしょうか? コメントアウトした状態で再起動、アクセスしてみましたがエラーログは変わっていない状態です(m_ _m)
begenner

2019/10/17 03:42

Railsしか使っていないのでわかりませんが ブラウザ上のエラーメッセージが Internal server error nginxのログメッセージが rewrite or internal redirection cycle while redirect to named location "@app", client: x.x.x.x, server: domainName, request: "GET / HTTP/1.1", host: "domainName" に変わりました。 /etc/nginx/conf.d/webapp.conf の下記の部分を変更しました server{ : try_files $uri/index.html $uri @app; location @app { try_files $uri/index.html $uri @app; <- 追加しました :
yu_1985

2019/10/17 03:52

そこに追加するのは違うような…。循環参照みたいになっているような気がします。 そこではなく上の方のtry_filesにきちんとrailsのindexに当たるものが記載されておりますでしょうか。
begenner

2019/10/17 06:10

何とお礼を言って良いのかわかりませんが、 こちらの質問はあまりにも長くなりすぎているため別枠で新規質問を建てさせていただきます。 https://teratail.com/questions/217743 こちらの質問内容は nginx の warning エラーの解決法として変更させていただきますのでよろしくお願い申し上げます(m_ _m)
guest

0

httpでRailsは動いているのでしょうか?

webapp.conf の location @app に

proxy_pass http://app_server;

の記述がないので

upstream app_server { # for UNIX domain socket setups server unix:/var/www/rails/webapp/tmp/sockets/.unicorn.sock fail_timeout=0; }

が参照されてない気がします。

環境構築する際には下記の順番に実施すると、原因がどこにあるか特定しやすくなります。
1.httpでwebサイトが表示される
2.httpでRailsが動く
3.httpsでwebサイトが表示される
4.httpsでRailsが動く

投稿2019/10/17 06:33

yukky1201

総合スコア2751

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

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

begenner

2019/10/17 06:50

1 、 2 までは動作確認が取れていました。 3に関しては location @app の中に proxy_pass http://app_server; を追加するとブラウザ上では We're sorry, but something went wrong. が表示されました。 nginx のエラーログでは [error] 9028#0: *6 connect() to unix:/var/www/rails/webapp/tmp/sockets/.unicorn.sock failed (111: Connection refused) while connecting to upstream, client: my_global_ip, server: myDomain, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/rails/webapp/tmp/sockets/.unicorn.sock:/", host: "myDomain" と表示されます。 現状がわからないのですが、 ・3 までは通過している ・4 がうまくいっていない といった感じでしょうか?
begenner

2019/10/17 06:54

ご回答いただいておいて大変申し訳ありませんが、 現在別URLにて前タイトルの質問をさせていただいています。 RailsアプリをHTTPS化して公開したい(ブラウザ上で 403 Forbidden nginx /1.16.1) https://teratail.com/questions/217743 そちらにご回答いただけると助かります(m_ _m)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問