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

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

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

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

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サーバーのコンピュータリソースをレンタルできるサービスです。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

2363閲覧

デプロイをしているのですが405が出てしまいます。多分rootの設定の仕方が問題だと思うのですが、、、

yaha4967

総合スコア106

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

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サーバーのコンピュータリソースをレンタルできるサービスです。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2021/05/05 06:49

編集2021/05/05 08:53

もともと、デプロイはできていたのですが、リロードすると”ページが見つかりません”となるエラーを治すために設定をいじっていたらAPIにアクセスできなくなってしまいました。

環境

イメージ説明
React+Rails APIモードをEC2にデプロイしています。
アプリサーバーにはunicorn
ウェブサーバーにnginxを使っています。

Rails
アプリ名:PuAReview
ルート:(ドメイン)/api/v1~

React
アプリ名:frontend
ルート:(ドメイン)/~

症状

症状としては

・APIへのPOSTをすると(APIのURL)405 (Not Allowed)とコンソールに出る
・APIのURLを検索窓に入れると、JSONではなくReactのレンダリングがされる
・”HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME”が常に出ている

という感じです。

僕のつけたあたり

ほとんど、nginxの設定をいじっていたので、nginxの設定をおかしくしてしまったんじゃないかなと予想しています。

で、もしかしたらRails側のrootを設定してないからなのかな?と思うのですが、同じサーバー名に2つrootを設定するとconflicting server name errorが発生してしまいます。

Nginxの設定

/etc/nginx/conf.d/(railsのapp名).conf

# loog directory error_log /var/www/PuAReview/log/nginx.error.log; access_log /var/www/PuAReview/log/nginx.access.log; # max body size client_max_body_size 2G; upstream app_server { # for UNIX domain socket setups server unix:/var/www/PuAReview/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; server_name 52.193.23.14; # nginx so increasing this is generally safe... keepalive_timeout 5; # path for static files root /var/www/PuAReview/frontend/build; #added # page cache loading try_files $uri /index.html; 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 /var/www/PuAReview/public; #自分のアプリケーション名に変 } } }

/etc/nginx/nginx.conf

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /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; 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 / { try_files $uri /index.html; } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

/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; # error_page 405 =200 $uri; #I am added this code # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

どこが間違っているか分かる方いらっしゃいましたらヒント教えていただきたいです。よろしくお願いします。

追記

rootの設定してみました!
結論からいうとrootつけてみましたが、変化なしでした。。。

試したこと

/etc/nginx/conf.d

にfrontend.confを追加

frontend.conf

server { listen 80; server_name 52.193.23.14; index index.html; access_log /var/log/nginx/frontend.access.log; error_log /var/log/nginx/frontend.error.log; location / { root /var/www/PuAReview/frontend/build; try_files $uri /index.html =404; } }

このままだとserver name conflictしてしまうので、PuAReview.confのlistenを81に変更。
あと、location 〇〇{}の意味が〇〇のURLにアクセスしたとき{}内のファイルにアクセスするという意味ぽいのでこう書いてみました。

server { listen 81; server_name 52.193.23.14; # nginx so increasing this is generally safe... keepalive_timeout 5; # path for static files #root /var/www/PuAReview/frontend/build; #added location /api/v1{ root /var/www/PuAReview/public; } #root /var/www/PuAReview/frontend/build; # page cache loading try_files $uri /index.html; 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; } # Rails error pages error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/PuAReview/public; #自分のアプリケーション名に変 } }

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

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

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

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

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

guest

回答1

0

自己解決

Railsのデプロイってnginxの設定がAPIモードと通常とでは違うようで、IP,とポートがローカルでいいっぽいです。たぶん。

https://www.yoheim.net/blog.php?q=20180407

投稿2021/05/05 09:17

yaha4967

総合スコア106

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問