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

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

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

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

nginx

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

Ruby on Rails

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

Amazon EC2

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

AWS(Amazon Web Services)

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

Q&A

解決済

1回答

2928閲覧

[デプロイ]Rails+Nginx+Unicorn+AWSで403Forbidden。

退会済みユーザー

退会済みユーザー

総合スコア0

unicorn

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

nginx

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

Ruby on Rails

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

Amazon EC2

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

AWS(Amazon Web Services)

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

0グッド

1クリップ

投稿2017/08/10 04:38

編集2017/08/10 04:39

現状

イメージ説明

基本的にRailsアプリの/publicディレクトリがforbiddenとなり、
nginxの設定が不足しており、Railsアプリのルートに行き届いていない。
またtry_filesを駆使することで、Railsアプリを表示できると目論んでいます

sudo tail -f /var/log/nginx/error.log

2017/08/10 04:32:18 [error] 4267#0: *7 directory index of "/var/www/projects/offisee(※アプリ名)/public/" is forbidden, client: 125.0.109.71, server: 13.113.34.47, request: "GET / HTTP/1.1", host: "13.113.34.47

インフラに詳しいかた、ぜひご助力お願いできませんか。

現在のNginxとUnicornの設定ファイル

初心者向け:AWS(EC2)にRailsのWebアプリをデプロイする方法 目次
の①から⑤までを参考にし、同じように設定を行いました。資料の通りELB/RDSを利用しています。

/etc/nginx/nginx.conf

server_namerootのみ編集しました。わかりやすいよう★をつけました。

# 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 /var/run/nginx.pid; # Load dynamic modules. See /usr/share/doc/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; index index.html index.htm; server { listen 80 default_server; listen [::]:80 default_server; server_name 13.113.34.47; ← ★ EC2のElasticIPに変更 root /var/www/projects/offisee/public; ← ★ /conf.d/{アプリ名}.confと同じものに変更 # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } }

/etc/nginx/nginx.conf.default

デフォルトのままです。

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } }

/etc/nginx/conf.d/{アプリ名}.conf

初心者向け:AWS(EC2)にRailsのWebアプリをデプロイする方法 目次
の①から⑤までを参考にし、同じように設定を行いました。

upstream unicorn_server { server unix:/var/www/projects/offisee/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; client_max_body_size 4G; server_name 52.199.206.175; keepalive_timeout 5; # Location of our static files root /var/www/projects/offisee/public; location ~ ^/assets/ { root /var/www/projects/offisee/public; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://unicorn_server; break; } } error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/projects/offisee/public; } }

/var/www/projects/{アプリ}/config/unicorn.conf.rb

初心者向け:AWS(EC2)にRailsのWebアプリをデプロイする方法 目次
の①から⑤までを参考にし、同じように設定を行いました。

# set lets $worker = 2 $timeout = 30 $app_dir = "/var/www/projects/offisee" $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 # set config worker_processes $worker working_directory $app_dir stderr_path $std_log stdout_path $std_log timeout $timeout listen $listen pid $pid # loading booster preload_app true # before starting processes 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 finishing processes after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end

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

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

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

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

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

guest

回答1

0

ベストアンサー

前提

NGINXの設定ファイルにおいては、serverのブロックを複数記述でき、これにより一つのサーバーで複数のドメイン名(VirtualHOSTと読んだりする)を収容することができます。
該当のserverブロックの内容が反映されるのは、server_nameに記載された、HOSTヘッダーを含むリクエスト(単純に該当のドメインでブラウザからアクセスしたということでOK)が来た場合です。

今回のエラーについて

NGINXの設定ファイルの、server_nameの値について、

  • /etc/nginx/nginx.confは、13.113.34.47
  • /etc/nginx/conf.d/{アプリ名}.confは、52.199.206.175

となっています。

この場合、http://13.113.34.47/へアクセスした場合は、/etc/nginx/nginx.confで設定しているサーバー設定の内容が反映されます(/etc/nginx/conf.d/{アプリ名}.confは関係しません)。

添付されたエラーログを見ると、server: 13.113.34.47,と記述がありますので、やはり/etc/nginx/nginx.confで設定しているVirtualHOSTへ接続しているようです。

http://52.199.206.175/でアクセスするとアプリ側の設定を書いてある方のVirtualHOSTへ接続できるのではないでしょうか?

なお、/etc/nginx/nginx.conf.defaultについては、/etc/nginx/nginx.confから読み込み(include)されていないようなので、記載内容は反映されません。

補記

/etc/nginx/nginx.confの上記指定箇所では、listen 80 default_server;としてあるので、特に指定していないドメイン名でアクセスがあると、この設定が優先されます。default_serverだから。

投稿2017/08/15 01:35

kacchan822

総合スコア57

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問