前提・実現したいこと
nginxとunicorn用いてruby on rails のproduction環境を実行しようとしています。
nginxとunicornを起動して、指定したURLにアクセスすると、
ブラウザでは「応答時間が長すぎます。」と表示されます。
また、サーバ上のターミナルでcurl -i
コマンドにて接続すると、
「HTTP/1.1 301 Moved Permanently」が発生します。
nginxとunicornの連携ができていないと思われます。
どうぞよろしくお願いします。
発生している問題・エラーメッセージ
$ curl -i http://<IPアドレス>:80/ HTTP/1.1 301 Moved Permanently Server: nginx/1.14.1 Date: Mon, 06 May 2019 15:05:59 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Location: https://<IPアドレス>/
railsプロジェクト config/unicorn.rb
ruby
1$worker = 2 2$timeout = 30 3$app_dir = File.expand_path('..', __dir__) 4$listen = File.expand_path 'tmp/sockets/unicorn.sock', $app_dir 5$pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir 6$std_log = File.expand_path 'log/unicorn.log', $app_dir 7 8# set config 9worker_processes $worker 10working_directory $app_dir 11stderr_path $std_log 12stdout_path $std_log 13timeout $timeout 14listen $listen 15pid $pid 16 17# loading booster 18preload_app true 19 20# before starting processes 21before_fork do |server, _worker| 22 defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! 23 old_pid = "#{server.config[:pid]}.oldbin" 24 if old_pid != server.pid 25 begin 26 Process.kill 'QUIT', File.read(old_pid).to_i 27 rescue Errno::ENOENT, Errno::ESRCH 28 end 29 end 30end 31 32# after finishing process 33after_fork do |_server, _worker| 34 defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection 35end
/etc/nginx/conf.d/sample.conf
conf
1# max body size 2client_max_body_size 2G; 3upstream app_server { 4 # for UNIX domain socket setups 5 server unix:{app_root}/tmp/sockets/unicorn.sock fail_timeout=0; 6} 7 8upstream unicorn { 9 server unix:{app_root}/tmp/sockets/unicorn.sock; 10} 11 12server { 13 listen 80; 14 server_name <IP アドレス>; 15 # nginx so increasing this is generally safe... 16 keepalive_timeout 5; 17 18 access_log {app_root}/log/nginx_access.log; 19 error_log {app_root}/log/nginx_error.log; 20 21 # path for static files 22 root {app_root}/public; 23 # page cache loading 24 try_files $uri/index.html $uri.html $uri @app; 25 location @app { 26 # HTTP headers 27 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 28 proxy_set_header Host $http_host; 29 proxy_redirect off; 30 proxy_pass http://app_server; 31 } 32 # Rails error pages 33 error_page 500 502 503 504 /500.html; 34 location = /500.html { 35 root {app_root}/public; 36 } 37}
補足情報(FW/ツールのバージョンなど)
Rails 5.2.3
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
unicorn (5.5.0.1.g6836, 5.5.0, 5.4.1)
unicorn-rails (2.2.1)
nginx version: nginx/1.14.1
Amazon Linux AMI 2018.03 Release Notes
回答1件
あなたの回答
tips
プレビュー