React / Django Rest Framework / nginx / uWSGI の本番環境設定方法
現在、Django Rest Framework と React を用いて開発を行っており、開発環境で動作した通信が本番環境でテストすると動かなくなり、困っています。
1台の物理サーバーでフロントエンド・バックエンドの両方を完結させようと試みています。
本番環境
- 物理サーバー: Ubuntu20.04
- WEBサーバー: nginx/1.18.0 (Ubuntu)
- アプリケーションサーバー: uwsgi 2.0.20
- django: 3.2.13
- node.js: v16.13.0
実現したいこと
- axios 等の http ライブラリを用いて、django 側とのデータの送受信を行いたい
発生している問題・エラーメッセージ
react から axios を用いてバックエンドの Django に対して通信を行おうとした際に、
net::ERR_CONNECTION_TIMED_OUT
となってしまいます。
POST http://(物理サーバーIPアドレス):8000/api/ net::ERR_CONNECTION_TIMED_OUT
該当のソースコード
python
1# mysite/settings.py 2 3... 4 5DEBUG = False 6ALLOWED_HOSTS = ["(物理サーバーのIPアドレス)", "localhost", "127.0.0.1"] 7 8... 9 10MIDDLEWARE = [ 11 'django.middleware.security.SecurityMiddleware', 12 'django.contrib.sessions.middleware.SessionMiddleware', 13 'django.middleware.common.CommonMiddleware', 14 'django.contrib.auth.middleware.AuthenticationMiddleware', 15 'django.contrib.messages.middleware.MessageMiddleware', 16 'django.middleware.clickjacking.XFrameOptionsMiddleware', 17 "corsheaders.middleware.CorsMiddleware", 18 "django.middleware.common.CommonMiddleware", 19 #'django.middleware.csrf.CsrfViewMiddleware',# ←JWT認証を使っているので外しています。 20] 21 22CORS_ALLOWED_ORIGINS = [ 23 "http://localhost", 24 "http://127.0.0.1", 25 "http://(物理サーバーのIPアドレス)", 26] 27 28WSGI_APPLICATION = 'mysite.wsgi.application' 29 30... 31
javascript
1// 該当の関数 2const hoge = (hoge) => { 3 return axios ({ 4 method: 'post', 5 mode: 'cors', 6 url: BASEURL + 'api/', 7 headers: { 8 'Content-Type':'application/json' 9 }, 10 data: { 11 'hoge':hoge 12 } 13 }) 14 .then(function (response) { 15 ... 16 .catch(error => { 17 ... 18 }) 19}
nginx
1# /etc/nginx/nginx.conf 2 3user www-data; 4worker_processes auto; 5pid /run/nginx.pid; 6include /etc/nginx/modules-enabled/*.conf; 7 8events { 9 worker_connections 768; 10} 11 12http { 13 sendfile on; 14 tcp_nopush on; 15 types_hash_max_size 2048; 16 include /etc/nginx/mime.types; 17 default_type application/octet-stream; 18 19 ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; 20 ssl_prefer_server_ciphers on; 21 access_log /var/log/nginx/access.log; 22 error_log /var/log/nginx/error.log; 23 24 gzip on; 25 server_tokens off 26 27 include /etc/nginx/conf.d/*.conf;# ← ここから以下の react.conf を読んでいます。 28 include /etc/nginx/sites-enabled/*; 29}
nginx
1# /etc/nginx/conf.d/react.conf 2 3server { 4 listen 80; 5 listen [::]:80; 6 7 server_name (物理サーバーのIPアドレス); 8 charset utf-8; 9 root /home/ubuntu/(reactのディレクトリ)/build; 10 index index.html index.htm; 11 12 location / { 13 proxy_set_header Host $http_host; 14 try_files $uri $uri/ /index.html; 15 16 allow (物理サーバーのIPアドレス) 17 allow 127.0.0.1; 18 deny all; 19 } 20}
bash
1# uWSGI 起動時のコマンド 2uwsgi --socket :8000 --module mysite.wsgi --pcre-jit --py-autoreload 1 --logto /tmp/uwsgi.log
得られる出力
python
1# uWSGI のログ(/tmp/uwsgi.log) 2 3*** Starting uWSGI 2.0.20 (64bit) on [Tue Oct 18 19:32:10 2022] *** 4compiled with version: 11.2.0 on 18 October 2022 01:29:56 5os: Linux-5.15.0-1020-aws #24-Ubuntu SMP Thu Sep 1 16:04:17 UTC 2022 6nodename: ip-10-0-10-10 7machine: x86_64 8clock source: unix 9pcre jit enabled 10detected number of CPU cores: 4 11current working directory: /home/ubuntu/(djangoのプロジェクトディレクトリ) 12detected binary path: /home/ubuntu/.pyenv/versions/3.9.9/bin/uwsgi 13your processes number limit is 62768 14your memory page size is 4096 bytes 15detected max file descriptor number: 1024 16lock engine: pthread robust mutexes 17thunder lock: disabled (you can enable it with --thunder-lock) 18uwsgi socket 0 bound to TCP address :8000 fd 3 19Python version: 3.9.9 (main, Sep 1 2022, 00:06:12) [GCC 11.2.0] 20Python main interpreter initialized at 0x55e4f544c790 21python threads support enabled 22your server socket listen backlog is limited to 100 connections 23your mercy for graceful operations on workers is 60 seconds 24mapped 145840 bytes (142 KB) for 1 cores 25*** Operational MODE: single process *** 26WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55e4f544c790 pid: 253294 (default app) 27*** uWSGI is running in multiple interpreter mode *** 28spawned uWSGI master process (pid: 253294) 29spawned uWSGI worker 1 (pid: 253324, cores: 1) 30Python auto-reloader enabled
javascript
1// ブラウザコンソールの出力 2Failed to load resource: net::ERR_CONNECTION_TIMED_OUT
/var/log/nginx/error.log に出力はなく、/var/log/nginx/access.log に通常のアクセスが記載されているのみでした。
試したこと
①権限の書き換え
bash
1sudo chmod o+x /home/ubuntu/
→ /var/log/nginx/error.log を確認する限り、(13: Permission denied) のエラーがなくなりました。
②reactの再ビルド
axios でアクセスする際のアドレスを以下のように変更しました。
- (開発環境時): http://localhost:8000
- (変更後): http://(物理サーバのIPアドレス):8000
補足
(1) react のアプリケーションディレクトリは django の プロジェクトディレクトリ の外に配置しています
.
├── django
│ ├── mysite(プロジェクトディレクトリ)
│ ├── settings.py
...
│ ├── myapp
│ ├── いろいろ
... ... ...
├── react
... ... ...
(2) react アプリケーションのビルドは以下のコマンドを用いて行いました。
bash
1npm run build
※知識が浅いので不要な情報でしたら申し訳ありません。スルーしてください。
ひとこと
axios を用いてデータの送受信を行う方法を探しています。
nginx や uWSGI についての知識が特に浅く、非常に困っています。
上記の問題の解決方法案が思い浮かぶ方がいらっしゃいましたら、ご教示いただけると幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー