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

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

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

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

nginx

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

Ruby on Rails

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

VPS

VPS(バーチャル・プライベート・サーバ)は、仮想化されたサーバをレンタルするサービスで、共有サーバでありながら専門サーバと同等の機能を果たします。物理的な専門サーバより安価で提供できるメリットがあります。

Q&A

2回答

4488閲覧

Nginx + Unicorn でRailsアプリが403 forbiddenエラーになる

ngtrcode

総合スコア8

unicorn

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

nginx

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

Ruby on Rails

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

VPS

VPS(バーチャル・プライベート・サーバ)は、仮想化されたサーバをレンタルするサービスで、共有サーバでありながら専門サーバと同等の機能を果たします。物理的な専門サーバより安価で提供できるメリットがあります。

0グッド

0クリップ

投稿2016/12/24 04:02

編集2016/12/26 11:26

お世話になります。

現在RailsアプリをVPSによって稼働させようとしています。

しかし、Railsアプリを表示させようとしたところ403 forbiddenが出てしまい、うまく稼働させることができません。

基本的な構成は、以下のように考えています。
##基本構成

  • /home/【user】/railsapp - APProot (【user】は実際のuser nameが入る。設定ファイル中も同様)
  • WebサーバーとしてNginx
  • Rack WebサーバとしてUnicorn
  • Ruby 2.3.1 (下記Railsアプリに必要仕様に合わせています。)
  • PostgreSQL 9.4 (下記Railsアプリに必要仕様に合わせています。)
  • OS - Ubuntu 14.04

です。

また、Railsアプリは、オープンソースのクラウドファンディングアプリを入れようと思っています。(https://github.com/catarse/catarse)

すごく非効率なのだと思いますが、sshでサーバにログインして、必要なものを構築していっているので、デプロイツール等は使っていません。

現在の各種設定ファイルは以下の通りです。
(コメントアウトされている部分は、文字数の制限により、一部削除しています。)

##Nginx設定

/etc/nginx/nginx.conf

user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## 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; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Unicorn Settings ## upstream unicorn { server unix:/home/【user】/railsapp/tmp/sockets/unicorn.sock fail_timeout=0; } ## # Gzip Settings ## gzip on; gzip_disable "msie6"; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }

/etc/nginx/sites-available/default

# Default server configuration # server { listen 80; root /home/【user】/railsapp; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } location /html/ { try_files $uri/index.html $uri.html $uri @unicorn; } location ~ ^/assets/ { alias /home/【user】/railsapp/public/assets; } location @unicorn { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://unicorn; } error_page 500 502 503 504 /500.html; location = /500.html { root /home/【user】/railsapp/pubilc; } #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # 文字数制限により削除してあります。

##unicorn設定ファイル

/home/【user】/railsapp/config/unicorn.rb

if ENV['WORKER_PROCESSES'] worker_processes ENV['WORKER_PROCESSES'].to_i else worker_processes 2 end user '【user】', '【user】' # Requests with more than 30 sec will be killed timeout 300 working_directory "/home/【user】/railsapp" # Preload entire app for fast forking. preload_app true listen "/home/【user】/railsapp/tmp/sockets/unicorn.sock" pid "/home/【user】/railsapp/tmp/pids/unicorn.pid" before_fork do |server, worker| Signal.trap 'TERM' do puts 'Unicorn master intercepting TERM and sending myself QUIT instead' Process.kill 'QUIT', Process.pid end defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! end after_fork do |server, worker| Signal.trap 'TERM' do puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT' end defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end stderr_path File.expand_path('log/unicorn-err.log', ENV['RAILS_ROOT']) stdout_path File.expand_path('log/unicorn-out.log', ENV['RAILS_ROOT'])

/etc/init.d/unicorn

#!/bin/sh # File: /etc/init.d/unicorn ### BEGIN INIT INFO # 文字数制限により削除 ### END INIT INFO # Feel free to change any of the following variables for your app: USER=【user】 #APP_ROOT=[PATH_TO_RAILS_ROOT_FOLDER] APP_ROOT=/home/【user】/railsapp # Set the environment. This can be changed to staging or development for staging # servers. RAILS_ENV=production # This should match the pid setting in $APP_ROOT/config/unicorn.rb. PID=$APP_ROOT/tmp/pids/unicorn.pid # A simple description for service output. DESC="Unicorn app - $RAILS_ENV" # If you're using rbenv, you may need to use the following setup to get things # working properly: RBENV_RUBY_VERSION=`cat $APP_ROOT/.ruby-version` RBENV_ROOT="/usr/local/rbenv" PATH="$RBENV_ROOT/bin:$PATH" SET_PATH="cd $APP_ROOT && rbenv rehash && rbenv local $RBENV_RUBY_VERSION" # Unicorn can be run using `bundle exec unicorn` or `bin/unicorn`. #UNICORN="bin/unicorn" UNICORN="bundle exec unicorn" # Execute the unicorn executable as a daemon, with the appropriate configuration # and in the appropriate environment. UNICORN_OPTS="-c $APP_ROOT/config/unicorn.rb -E $RAILS_ENV -D" CMD="$SET_PATH && $UNICORN $UNICORN_OPTS" # Give your upgrade action a timeout of 60 seconds. TIMEOUT=60 # Store the action that we should take from the service command's first # argument (e.g. start, stop, upgrade). action="$1" # Make sure the script exits if any variables are unset. This is short for # set -o nounset. set -u # Set the location of the old pid. The old pid is the process that is getting # replaced. old_pid="$PID.oldbin" # Make sure the APP_ROOT is actually a folder that exists. An error message from # the cd command will be displayed if it fails. cd $APP_ROOT || exit 1 # A function to send a signal to the current unicorn master process. sig () { test -s "$PID" && kill -$1 `cat $PID` } # Send a signal to the old process. oldsig () { test -s $old_pid && kill -$1 `cat $old_pid` } # A switch for handling the possible actions to take on the unicorn process. case $action in # Start the process by testing if it's there (sig 0), failing if it is, # otherwise running the command as specified above. start) sig 0 && echo >&2 "$DESC is already running" && exit 0 su - $USER -c "$CMD" ;; # Graceful shutdown. Send QUIT signal to the process. Requests will be # completed before the processes are terminated. stop) sig QUIT && echo "Stopping $DESC" exit 0 echo >&2 "Not running" ;; # Quick shutdown - kills all workers immediately. force-stop) sig TERM && echo "Force-stopping $DESC" && exit 0 echo >&2 "Not running" ;; # Graceful shutdown and then start. restart) sig QUIT && echo "Restarting $DESC" && sleep 2 \ && su - $USER -c "$CMD" && exit 0 echo >&2 "Couldn't restart." ;; # Reloads config file (unicorn.rb) and ... reload) sig HUP && echo "Reloading configuration for $DESC" && exit 0 echo >&2 "Couldn't reload configuration." ;; # Re-execute the running binary, ... upgrade) if sig USR2 && echo "Upgrading $DESC" && sleep 10 \ && sig 0 && oldsig QUIT then n=$TIMEOUT while test -s $old_pid && test $n -ge 0 do printf '.' && sleep 1 && n=$(( $n - 1 )) done echo if test $n -lt 0 && test -s $old_pid then echo >&2 "$old_pid still exists after $TIMEOUT seconds" exit 1 fi exit 0 fi echo >&2 "Couldn't upgrade, starting 'su - $USER -c \"$CMD\"' instead" su - $USER -c "$CMD" ;; # A basic status checker. Just checks if the master process is responding to # the `kill` command. status) sig 0 && echo >&2 "$DESC is running." && exit 0 echo >&2 "$DESC is not running." ;; # Reopen all logs owned by the master and all workers. reopen-logs) sig USR1 ;; # Any other action gets the usage message. *) # Usage echo >&2 "Usage: $0 <start|stop|restart|reload|upgrade|force-stop|reopen-logs>" exit 1 ;; esac

以上が設定ファイルです。
##nginxエラーログ

nginxのエラーログは下記の通りです。

/var/log/nginx/error.log

2016/12/24 10:36:45 [error] 24421#24421: *16 directory index of "/home/【user】/railsapp/" is forbidden, client: 153.229.108.244, server: _, request: "GET / HTTP/1.1", host: "27.120.80.169"

##権限設定
forbiddenとでているので、Approotのオーナー、権限を調べてみましたが、以下の通りです。

# /home/【user】にて $ls -l drwxrwxr-x 18 www-data www-data 4096 Dec 22 21:31 railsapp # /home/【user】/railsappにて $ls -l -rwxrwxr-x 1 www-data www-data 3437 Dec 22 20:01 Gemfile -rwxrwxr-x 1 www-data www-data 18185 Dec 22 20:01 Gemfile.lock -rwxrwxr-x 1 www-data www-data 343 Dec 22 20:01 Rakefile drwxrwxr-x 13 www-data www-data 4096 Dec 22 20:01 app -rwxrwxr-x 1 www-data www-data 501 Dec 22 20:01 app.json drwxrwxr-x 2 www-data www-data 4096 Dec 22 20:01 bin -rwxrwxr-x 1 www-data www-data 781 Dec 22 20:01 bower.json -rwxrwxr-x 1 www-data www-data 270 Dec 22 20:01 circle.yml drwxrwxr-x 5 www-data www-data 4096 Dec 24 00:10 config -rwxrwxr-x 1 www-data www-data 255 Dec 22 20:01 config.ru drwxrwxr-x 4 www-data www-data 4096 Dec 24 00:21 db drwxrwxr-x 2 www-data www-data 4096 Dec 22 20:01 deploy drwxrwxr-x 4 www-data www-data 4096 Dec 22 20:01 lib drwxrwxr-x 2 【user】 【user】 4096 Dec 23 18:17 log drwxrwxr-x 4 www-data www-data 4096 Dec 22 21:20 node_modules -rwxrwxr-x 1 www-data www-data 192 Dec 22 20:01 package.json drwxrwxr-x 4 www-data www-data 4096 Dec 22 21:23 public drwxrwxr-x 19 www-data www-data 4096 Dec 22 20:01 spec drwxrwxr-x 5 www-data www-data 4096 Dec 23 17:40 tmp drwxrwxr-x 5 www-data www-data 4096 Dec 22 20:22 vendor

現状、以上の設定で403forbiddenがでます。ご助言いただけますと幸いです。
なお、VPSをいじるのは、初めての経験ですので、必要な情報がまだ足りないかもしれません。
その際には、何の情報があると、よりご教示いただけるということを教えていただきたく思います。

よろしくお願いいたします。

【12/26追記】
publicディレクトリに、index.htmlを配置したところ、これ自体は表示することができました。

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

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

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

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

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

guest

回答2

0

もし違ったら、すいませんが、【】が悪さをしている可能性があると思います。
ディレクトリに【】を使っているのは何故でしょうか?
もし、必須要件ではないのであれば、【】を外したディレクトリの環境を用意して試してみてはどうでしょうか?

投稿2016/12/26 10:33

IPU

総合スコア283

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

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

ngtrcode

2016/12/26 10:58

ご回答ありがとうございます。 すみません、こちらの【user】というのは、teratail上での表示です。実際には、【】はついておらず、/home/hoge/railsapp のようになっております。 わかりづらい表記ですみません!
IPU

2016/12/26 11:01

あー、やっぱり、そんな変な構成にはしてないですよね。 了解しました。
guest

0

location / で @unicorn 動かしてないですが問題ないですか?

投稿2016/12/24 17:28

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

ngtrcode

2016/12/25 06:21

ありがとうございます。 location / で @unicornを記述し、 他のlocation設定で、@unicornの記述を消して、nginxを再起動して試して見ましたが、まだ、403forbiddenエラーが続いています。
退会済みユーザー

退会済みユーザー

2016/12/25 15:30

すいません、わかりません。 sites-enabled/ には設定がコピーなりされているのは間違いないですか? あと root は .../public/ になるかと思います。
ngtrcode

2016/12/26 08:17

tanyzzさん、ありがとうございます。 sites-enabled/defaultにsites-available/defaultのシンボリックリンクがはられています。 $ ls -l (..../sites-enabled/で) lrwxrwxrwx 1 root root 34 Dec 20 16:24 default -> /etc/nginx/sites-available/default rootに関する設定についても教えていただきありがとうございます。
退会済みユーザー

退会済みユーザー

2016/12/26 14:57

私が試すとしたら、 listen 3000 とかで unicorn が単独で動作するか確認するとか selinux も disabled にしてみるとかでしょうか。
ngtrcode

2016/12/26 15:17

なるほど、そういう切り分けかたもあるのですね。勉強になります。 ちょっと試してみます!
退会済みユーザー

退会済みユーザー

2016/12/26 15:20

同じような設定で試しましたが、私の環境ではやっぱり location / 直せば動きますね。 location / { try_files $uri $uri/ @unicorn; }
退会済みユーザー

退会済みユーザー

2016/12/26 15:27

それと assets が forbidden になるので location /assets/ { root /home/【user】/railsapp/public/assets; }
ngtrcode

2016/12/28 04:24

おや、tanyzzさん退会されてしまったんでしょうか・・・? 教えていただいた設定にしても403forbiddenになっているので、なにか根本的に間違っている部分がありそうです。たしかに、ローカルだとそれは動くのですが、VPS上だとうごかなくなってるので、対応してみようと考えています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問