もともと、デプロイはできていたのですが、リロードすると”ページが見つかりません”となるエラーを治すために設定をいじっていたら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; #自分のアプリケーション名に変 } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。