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

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

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

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

nginx

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

AWS(Amazon Web Services)

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

Q&A

解決済

2回答

3105閲覧

unicorn + nginx で作ったec2上のrailsアプリにアクセスすると301エラーが出て、何も表示されない

tomohiroo

総合スコア19

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

nginx

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

AWS(Amazon Web Services)

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

0グッド

0クリップ

投稿2018/09/14 13:55

編集2018/09/16 10:15

railsアプリを、capistranoを使ってaws ec2にデプロイしました。
webサーバはnginx, appサーバはunicornを使っています。
しかし、ipアドレスに curl 【ipアドレス】 などでアクセスしても、レスポンスが返って来ません。

/home/tomohiroo/pecopeco/shared/log/nginx.error.log/home/tomohiroo/pecopeco/shared/log/unicorn.log には、なにも表示されず、
(userはtomohiroo, appnameはpecopecoです。)

pecopeco/shared/log/nginx.access.log

xxx.xxx.xxx.xxx(クライアントのipアドレス) - - [15/Sep/2018:18:52:13 +0900] "GET / HTTP/1.1" 301 5 "-" "curl/7.54.0"

と, 301エラーのログが書いてあります。。

どうすれば、アクセスできるのでしょうか?

関係あるのかがよくわかっていないのですが、
➜ ~ echo -en 'GET / HTTP/1.0\r\n\r\n' | socat stdio /home/tomohiroo/pecopeco/current/tmp/sockets/unicorn.sock を叩くと

HTTP/1.1 301 Moved Permanently Date: Sun, 16 Sep 2018 10:14:09 GMT Connection: close Content-Type: text/html Location: https://localhost/ Vary: Origin

と出て来ます。

/etc/nginx/conf.d/pecopeco.confは以下です

error_log /home/tomohiroo/pecopeco/current/log/nginx.error.log; access_log /home/tomohiroo/pecopeco/current/log/nginx.access.log; client_max_body_size 2G; upstream app_server { server unix:/home/tomohiroo/pecopeco/current/tmp/sockets/unicorn.sock fail_timeout=0; } server { listen 80; server_name xx.xxx.xxx.xxx; keepalive_timeout 5; root /home/tomohiroo/pecopeco/current/public; # try_files $uri/index.html $uri.html $uri @app; location / { try_files $uri/index.html $uri.html $uri @app; } location @app { # HTTP headers proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } error_page 500 502 503 504 /500.html; location = /500.html { root /home/tomohiroo/pecopeco/current/public; } }

/etc/nginx/nginx.conf

# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }

以下ローカルのファイルです。

config/unicorn/production.rb

$worker = 2 $timeout = 30 $app_dir = "/home/tomohiroo/pecopeco/current" $listen = File.expand_path 'tmp/sockets/unicorn.sock', $app_dir $pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir $std_log = File.expand_path 'log/unicorn.log', $app_dir worker_processes $worker working_directory $app_dir stderr_path $std_log stdout_path $std_log timeout $timeout listen $listen pid $pid preload_app true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin Process.kill "QUIT", File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end

config/Capfile

require 'capistrano/setup' require 'capistrano/deploy' require 'capistrano/rbenv' require 'capistrano/bundler' require 'capistrano/rails/migrations' require 'capistrano/console' Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } require "capistrano/scm/git" install_plugin Capistrano::SCM::Git

config/deploy.rb

# config valid for current version and patch releases of Capistrano lock "~> 3.11.0" set :application, "pecopeco" set :repo_url, "git@github.com:nakamotoo/pecopeco.git" set :branch, 'master' set :deploy_to, '/home/tomohiroo/pecopeco' set :linked_files, %w{config/master.key config/secrets.yml} set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system} set :keep_releases, 5 set :rbenv_ruby, '2.5.1' set :log_level, :debug set :repo_tree, 'server' set :default_env, { JAVA_HOME: "/usr/java/jdk1.8.0_181-amd64" } namespace :deploy do desc 'Restart application' task :restart do invoke 'unicorn:restart' end desc 'Create database' task :db_create do on roles(:db) do |host| with rails_env: fetch(:rails_env) do within current_path do execute :bundle, :exec, :rake, 'db:create' end end end end desc 'Run seed' task :seed do on roles(:app) do with rails_env: fetch(:rails_env) do within current_path do execute :bundle, :exec, :rake, 'db:seed_fu' end end end end after :publishing, :restart after :restart, :clear_cache do on roles(:web), in: :groups, limit: 3, wait: 10 do end end end

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

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

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

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

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

guest

回答2

0

まず最初に ローカル環境などでは動いていると仮定して

2018/09/15 01:57:19 [crit] 19507#0: *1 stat() "/home/tomohiroo/pecopeco/current/public/hoge" failed (13: Permission denied), client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /hoge HTTP/1.1", host: "xxx.xxx.xxx.xxx"

2018/09/14 22:42:05 [crit] 31019#0: *643 connect() to unix:/home/tomohiroo/pecopeco/current/tmp/sockets/.unicorn.sock failed (13: Permission denied) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET / HTTP/1.1", upstream: "http://unix:/home/tomohiroo/pecopeco/current/tmp/sockets/.unicorn.sock:/", host: "xxx.xxx.xxx.xxx"

というエラーが出ていますが、nginx ユーザーは、 /home/tomohiroo/pecopeco にアクセスできますか?

linux のデフォルト設定だとホームディレクトリには、アクセスできないと思うので、アクセスできるように変更されましたか?

投稿2018/09/15 00:03

CHERRY

総合スコア25171

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

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

tomohiroo

2018/09/15 02:58 編集

ご返信ありがとうございます! 確かに、ホームディレクトリの権限は問題だったようでした! ただ、chmod 755 tomohirooするとForbiddenは出なくなりましたが、レスポンスが何も帰ってこなくなってしまいました。。! ~/pecopeco/shared/log/nginx.access.logには 124.213.120.152 - - [15/Sep/2018:11:42:36 +0900] "GET / HTTP/1.1" 301 5 "-" "curl/7.54.0" と出ますが、~/pecopeco/shared/log/production.logには何も出て来ません。。
tomohiroo

2018/09/15 02:58

また、ローカル環境はpumaで動かしています。
CHERRY

2018/09/15 03:06 編集

nginx で、/home/tomohiroo/pecopeco/current/tmp/sockets/.unicorn.sock を指定しているということは、Rails のアプリケーションサーバーとして、unicorn を使っているということだと思いますが、問題なく起動していて、アクセス可能でしょうか?
tomohiroo

2018/09/15 03:14

➜ ~ ps -ef | grep unicorn tomohir+ 26319 1 0 9月13 ? 00:01:37 unicorn master -c /home/tomohiroo/pecopeco/current/config/unicorn/production.rb -E production -D tomohir+ 26333 26319 0 9月13 ? 00:00:00 unicorn worker[0] -c /home/tomohiroo/pecopeco/current/config/unicorn/production.rb -E production -D tomohir+ 26335 26319 0 9月13 ? 00:00:00 unicorn worker[1] -c /home/tomohiroo/pecopeco/current/config/unicorn/production.rb -E production -D tomohir+ 29766 24947 0 12:10 pts/0 00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn unicorn 以上のように、unicornは起動していると思うのですが、アクセスできません。 アクセス可能かどうかとはcurl xxx.xxx.xxx.xxxした場合にレスポンスが返って来るかどうか、という認識なのですが、これで合っているでしょうか?
CHERRY

2018/09/15 04:06

unicorn の設定も不明なので、推測ですが、socket の指定が異なっているとかないでしょうか?
CHERRY

2018/09/15 04:15

デフォルトは、たしか rails_root_dir/tmp/sockets/unicorn.sock だったと思いますが、質問に記載された nginx の設定をみると、`/home/tomohiroo/pecopeco/current/tmp/sockets/.unicorn.sock ` が、指定されています。 `/home/tomohiroo/pecopeco/current/tmp/sockets/.unicorn.sock `のファイル名に `.` が付いているようですが、このあたりのミスで違うパスを参照していたりしないでしょうか?
tomohiroo

2018/09/15 04:38

ありがとうございます。 /etc/nginx/nginx.confの.unicorn.sockをunicorn.sockに変更し、 config/unicorn.conf.rbとconfig/unicorn/production.rbの $listen = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir を $listen = File.expand_path 'tmp/sockets/unicorn.sock', $app_dir にし、デプロイし直したところ、今度は <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> が返って来るようになりました。 unicornは起動しているよう(bundle exec unicorn -E production -c config/unicorn/production.rb -Dとするとpecopeco/shared/log/unicorn.logにArgumentError: Already running on PID:26319 (or pid=/home/tomohiroo/pecopeco/current/tmp/pids/unicorn.pid is staleが書かれる。)です。 重ね重ねもうしわけありません。。
tomohiroo

2018/09/15 08:49 編集

public下の静的ファイル(public/robots.txtなど)はレスポンスとして返って来ます。 production.logは何も書き込まれず、 shared/log/nginx.access.logには 2018/09/15 13:40:15 [crit] 8452#0: *5 connect() to unix:/home/tomohiroo/pecopeco/current/tmp/sockets/unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: xx.xxx.xxx.xxx, request: "GET / HTTP/1.1", upstream: "http://unix:/home/tomohiroo/pecopeco/current/tmp/sockets/unicorn.sock:/", host: "xx.xxx.xxx.xxx"
tomohiroo

2018/09/15 04:47

ファイル名がcurrent/tmp/sockets/.unicorn.sockのままでした。 (mvコマンドで名前を変更するとレスポンスが返ってこなくなりましたが、) 原因を調べます。
CHERRY

2018/09/15 04:48

unicorn.conf.rb で、`.unicorn.sock` にしているのであれば、 nginx の socket もそのまま( `.unicorn.sock` )で、 OK です。
CHERRY

2018/09/15 04:53

うちの環境の設定ファイルと比べてみたら ` try_files` が `location / { 〜} ` の中にいれてありますね。 何か変わるのかな...
tomohiroo

2018/09/15 04:58

.unicorn.sockに戻すともちろん前の通り、レスポンスが返ってこず、 location / { try_files ... } として、sudo systemctl restart nginxしてもレスポンスは返ってこず変わりませんでした。。。
tomohiroo

2018/09/15 05:00

pecopeco/shared/log/nginx.error.logにもなにもかかれないです。
tomohiroo

2018/09/15 08:48

pecopeco/shared/log/nginx.access.logに 122.135.122.235 - - [15/Sep/2018:17:44:15 +0900] "GET / HTTP/1.1" 301 5 "-" "curl/7.54.0" と301が吐かれます。
CHERRY

2018/09/15 09:00 編集

nginx -> unicorn の部分に問題があるのだと思いますが、 log/unicorn.log には、何か出ていますか?
tomohiroo

2018/09/15 09:11 編集

➜ ~ tail -f pecopeco/shared/log/unicorn.log from /home/tomohiroo/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/exe/bundle:30:in `block in <top (required)>' from /home/tomohiroo/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors' from /home/tomohiroo/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/exe/bundle:22:in `<top (required)>' from /home/tomohiroo/.rbenv/versions/2.5.1/bin/bundle:23:in `load' from /home/tomohiroo/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>' I, [2018-09-15T16:02:50.206615 #24186] INFO -- : Refreshing Gem list I, [2018-09-15T16:02:52.102958 #24186] INFO -- : listening on addr=/home/tomohiroo/pecopeco/current/tmp/sockets/unicorn.sock fd=16 I, [2018-09-15T16:02:52.116304 #24186] INFO -- : master process ready I, [2018-09-15T16:02:52.122179 #24201] INFO -- : worker=0 ready I, [2018-09-15T16:02:52.126198 #24203] INFO -- : worker=1 ready これしか出ていないです。。 nginxからunicornにうまく繋がっていないと言うことでしょうか。。
CHERRY

2018/09/15 09:19

色々試してもらったせいもあるのですが... unicorn のログには、socketファイル名が、unicorn.sock のようですが、nginx の socket のファイル名は同じになっていますよね?
tomohiroo

2018/09/15 09:50

すみません、本文を修正したせいです。 本文中の題名含め書き直します。
tomohiroo

2018/09/15 11:11 編集

同じになっていると思います。 色々試して状況が変わったので、本文を書き直しました。
guest

0

自己解決

何故なのかわかりませんが、capistrano3-unicornを使ったらできました。
自分で書いていたunicornのファイルが間違っていたようです。

投稿2018/09/17 06:14

tomohiroo

総合スコア19

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問