LoginSignup
1
0

More than 1 year has passed since last update.

VPSに検索系Djangoアプリをデプロイしたらタイムアウトしたので修正した際のメモ

Last updated at Posted at 2022-09-16

Conoha VPSにDjangoアプリをデプロイしました。
デプロイするにあたって参考にした記事はこちらです。

こちらの記事に沿って順に設定を行ったところデプロイはできたものの、
502 Bad Gateway
が発生しました。

エラーログを確認します。

vi /var/log/nginx/error.log

エラーログを確認すると、

2022/09/05 16:09:21 [error] 906#906: *575 upstream prematurely closed connection while reading response header from upstream,...

のエラーが出ていたのと、検索件数を少なくした場合に発生しなかったため、タイムアウトしているらしいということがわかりました。
タイムアウトの設定を追加する方法を確認します。

nginx

nginxの設定

/etc/nginx/nginx.conf

nginx起動時に読み込まれる主の設定ファイル。
このファイルの設定を変更してnginxを制御。

設定内容

httpディレクティブ配下に、serverディレクティブを配置し、

http {
    server {
            listen 80;
            server_name サーバーIP;
    
             location / {
                proxy_set_header Connection "";
                proxy_http_version 1.1;
              }
            }
        }
        keepalive_timeout 100;
    }
   (以下略)
}

keepalive_timeoutの設定がデフォルトでは0秒になっていたので、
こちらを100秒にしました。

次に、nginxを再起動し、起動状態を確認します。

systemctl restart nginx
systemctl status nginx

ちなみに、nginxを再起動時に、nginx.confの設定が間違っていると
エラーが起きて起動できないことがあります。

Job for nginx.service failed because the control process exited with error code.

これが出たら、

nginx -t 

を実行するとどこでエラーになっているのかわかります。

VPSに作成したサーバ自体を再起動するには、

reboot

を実施します。

次に、gunicornの起動を行います。

systemctl start gunicorn
systemctl status gunicorn.service

これでタイムアウトが発生しなくなりました。

あと、nginxを起動時にエラーが出るので

nginx: [warn] conflicting server name "xxx.xxx.xxx.xxx" on 0.0.0.0:80, ignored

ポート80が使われているようです。ポート80を使用しているサービスは何か、調べます。

sudo lsof -i:80

Apache2が起動していました。
Apache2が使うポートを変更します。

sudo vi /etc/apache2/ports.conf
Listen 8080

ポートを80から8080などに変更し、Apacheを再起動します。

sudo service apache2 restart
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0