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

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

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

Gunicorn (Green Unicorn)は、Rubyのunicornをベースに開発されたUNIX向けのPython製HTTPサーバです。他のライブラリとの依存関係がないため、容易にインストールして使用できます。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

nginx

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

7773閲覧

【Django】Nginx-Gunicorn間の502 Bad Gatewayについて

退会済みユーザー

退会済みユーザー

総合スコア0

Gunicorn

Gunicorn (Green Unicorn)は、Rubyのunicornをベースに開発されたUNIX向けのPython製HTTPサーバです。他のライブラリとの依存関係がないため、容易にインストールして使用できます。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

nginx

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/04/06 17:38

編集2019/04/07 06:54

現場で使えるDjangoの教科書 実践編のP.136以下の手順に基づいて、Ubuntu上のDjangoをリバースプロキシ(Nginx)、WSGIサーバ(Gunicorn)にデプロイしようとしていますが、次の問題が発生しております。分かりにくい点多々あると思いますが、よろしくお願いします。

  • 問題

Nginx、Gunicorn(xxxx.service, xxxx.socket)ともにsystemctl statusで確認したところ、
active(runnning)になっておりますが、Webサイトに接続したところ、
"502 Bad Gateway nginx/1.14.0 (Ubuntu)"
という表示がなされ、サイトに正常に接続できない状況です。

python3 manage.py runserverでは、正常にサイトが表示されるため、Djangoの問題ではないと考えております。

また、前提として、本書の手順では、リバースプロキシ(Nginx)とWSGIサーバ(Gunicorn)の通信手段として、INETドメインソケット(TCPソケット)ではなく、UNIXドメインソケットを採用しています。

  • 環境

サービス: AWS(EC2)
サーバー: Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1032-aws x86_64)
ユーザー: ubuntu, root, webapp(NginxとGunicornの実行ユーザー)
ソフトウェアのバージョン: nginx version: nginx/1.14.0 (Ubuntu), gunicorn (version 19.9.0)
ディレクトリの階層構造: /home/ubuntu/opt/csirt

(venv) ubuntu@ip-172-31-47-113:~/opt/csirt$ ll total 16 drwxr-xr-x 4 webapp webapp 4096 Apr 6 15:36 ./ drwxrwxr-x 3 ubuntu ubuntu 4096 Apr 6 15:30 ../ drwxr-xr-x 8 webapp webapp 4096 Apr 6 15:31 csirt/ ← csirt配下にDjangoのプロジェクトディレクトリが存在する。 drwxr-xr-x 6 webapp webapp 4096 Apr 6 15:36 venv/ ← source venv/bin/activateで仮想環境に入り、gunicornを実行。
  • 原因と思われる箇所

ソケット通信でNginxとGunicornが1つのソケットファイルを読み書きするファイルの権限が正常に設定されていないようです。しかし、どのようにファイル権限を変更したら、正常にデプロイできるのか分かりません。

  • 参考

参考までに関連すると思われる設定ファイルの中身を記載しています。

Gunicorn

1(venv) webapp@xxx:/home/ubuntu/opt/csirt/csirt$ ll /var/run/gunicorn/csirt.sock 2srw-rw-rw- 1 root root 0 Apr 7 00:38 /var/run/gunicorn/csirt.sock= 3(venv) webapp@xxx:/home/ubuntu/opt/csirt/csirt$

Nginxのエラーログです。Gunicornのソケットタイプが正常に起動していないのでしょうか。

errorlog

1(venv) webapp@ip-172-31-47-113:/home/ubuntu/opt/csirt/csirt$ sudo tail -f /var/log/nginx/error.log 22019/04/07 00:59:36 [alert] 25854#25854: *7 open socket #10 left in connection 3 32019/04/07 00:59:36 [alert] 25854#25854: aborting 42019/04/07 01:20:10 [crit] 26186#26186: *2 connect() to unix:/home/ubuntu/opt/csirt/csirt.sock failed (2: No such file or directory) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: 52.68.240.157, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/opt/csirt/csirt.sock:/", host: "52.68.240.157" 52019/04/07 01:25:32 [crit] 26186#26186: *4 connect() to unix:/home/ubuntu/opt/csirt/csirt.sock failed (2: No such file or directory) while connecting to upstream, client: xxx.xxx.xxx.xxx0, server: 52.68.240.157, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/opt/csirt/csirt.sock:/", host: "52.68.240.157"

systemctlの結果です。sudo systemctl status csirt.socket

socket

1(venv) webapp@xxx:/home/ubuntu/opt/csirt/csirt$ sudo systemctl status csirt.socket 2Failed to dump process list, ignoring: No such file or directory 3● csirt.socket - gunicorn socket (csirt) 4 Loaded: loaded (/etc/systemd/system/csirt.socket; enabled; vendor preset: enabled) 5 Active: active (running) since Sun 2019-04-07 00:38:07 JST; 1h 33min ago 6 Listen: /run/gunicorn/csirt.sock (Stream) 7 CGroup: /system.slice/csirt.socket 8 9Apr 07 00:38:07 ip-172-31-47-113 systemd[1]: Listening on gunicorn socket (csirt).

systemctlの結果です。sudo systemctl status csirt.service

service

1(venv) webapp@xxx:/home/ubuntu/opt/csirt/csirt$ sudo systemctl status csirt.service 2● csirt.service - gunicorn daemon (csirt) 3 Loaded: loaded (/etc/systemd/system/csirt.service; enabled; vendor preset: en 4 Active: active (running) since Sun 2019-04-07 00:38:07 JST; 1h 38min ago 5 Main PID: 25574 (gunicorn) 6 Tasks: 4 (limit: 1152) 7 CGroup: /system.slice/csirt.service 8 ├─25574 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csir 9 ├─25599 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csir 10 ├─25600 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csir 11 └─25602 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csir 12 13Apr 07 00:44:01 xxx gunicorn[25574]: - - [06/Apr/2019:15:44:01 +00 14Apr 07 00:52:43 xxx gunicorn[25574]: - - [06/Apr/2019:15:52:43 +00 15Apr 07 00:52:45 xxx gunicorn[25574]: - - [06/Apr/2019:15:52:45 +00 16Apr 07 00:52:48 xxx gunicorn[25574]: - - [06/Apr/2019:15:52:48 +00 17Apr 07 00:59:19 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:19 +00 18Apr 07 00:59:49 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:49 +00 19Apr 07 00:59:53 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:53 +00 20Apr 07 00:59:59 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:59 +00 21Apr 07 01:10:42 xxx gunicorn[25574]: - - [06/Apr/2019:16:10:42 +00 22Apr 07 01:24:42 xxx systemd[1]: csirt.service: Current command vani 23(venv) webapp@xxx:/home/ubuntu/opt/csirt/csirt$ sudo systemctl status csirt.service 24● csirt.service - gunicorn daemon (csirt) 25 Loaded: loaded (/etc/systemd/system/csirt.service; enabled; vendor preset: enabled) 26 Active: active (running) since Sun 2019-04-07 00:38:07 JST; 1h 38min ago 27 Main PID: 25574 (gunicorn) 28 Tasks: 4 (limit: 1152) 29 CGroup: /system.slice/csirt.service 30 ├─25574 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csirt/venv/bin/gunicorn --access-logfile - --workers 3 --bind u 31 ├─25599 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csirt/venv/bin/gunicorn --access-logfile - --workers 3 --bind u 32 ├─25600 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csirt/venv/bin/gunicorn --access-logfile - --workers 3 --bind u 33 └─25602 /home/ubuntu/opt/csirt/venv/bin/python3 /home/ubuntu/opt/csirt/venv/bin/gunicorn --access-logfile - --workers 3 --bind u 34 35Apr 07 00:44:01 xxx gunicorn[25574]: - - [06/Apr/2019:15:44:01 +0000] "GET /manager/html HTTP/1.0" 404 86 "-" "Mozilla/5.0 (W 36Apr 07 00:52:43 xxx gunicorn[25574]: - - [06/Apr/2019:15:52:43 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 37Apr 07 00:52:45 ip-172-31-47-113 gunicorn[25574]: - - [06/Apr/2019:15:52:45 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 38Apr 07 00:52:48 xxx gunicorn[25574]: - - [06/Apr/2019:15:52:48 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 39Apr 07 00:59:19 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:19 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 40Apr 07 00:59:49 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:49 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 41Apr 07 00:59:53 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:53 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 42Apr 07 00:59:59 xxx gunicorn[25574]: - - [06/Apr/2019:15:59:59 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (Macintosh; In 43Apr 07 01:10:42 xxx gunicorn[25574]: - - [06/Apr/2019:16:10:42 +0000] "GET / HTTP/1.0" 500 27 "-" "Mozilla/5.0 (iPhone; CPU i 44Apr 07 01:24:42 xxx systemd[1]: csirt.service: Current command vanished from the unit file, execution of the command list won' 45lines 1-21/21 (END)

systemctlの結果です。/etc/systemd/system/csirt.service

service

1[Unit] 2Description=gunicorn daemon (csirt) 3Requires=csirt.socket 4After=network.target 5 6[Service] 7User=webapp 8Group=www-data 9WorkingDirectory=/home/ubuntu/opt/csirt/csirt 10ExecStart=/home/ubuntu/opt/csirt/venv/bin/gunicorn \ 11 --access-logfile - \ 12 --workers 3 \ 13 --bind unix:/run/gunicorn/csirt.sock \ 14 csirt.wsgi:application 15 16[Install] 17WantedBy=multi-user.target

/etc/systemd/system/csirt.socket

[Unit] Description=gunicorn socket (csirt) [Socket] ListenStream=/run/gunicorn/csirt.sock [Install] WantedBy=sockets.target

/etc/nginx/sites-available/csirt

nginx

1server { 2 listen 80; 3 server_name xxx.xxx.xxx.xxx; 4 5 location = /favicon.ico { access_log off; log_not_found off; } 6 location /static/ { 7 root /var/www/csirt; 8 } 9 location / { 10 include proxy_params; 11 proxy_pass http://unix:/run/gunicorn/csirt.sock; 12 } 13}

/etc/nginx/proxy_params

proxy

1proxy_set_header Host $http_host; 2proxy_set_header X-Real-IP $remote_addr; 3proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 4proxy_set_header X-Forwarded-Proto $scheme;

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

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

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

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

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

guest

回答1

0

デプロイって大変ですよね。。
ソケットは立ち上がっているようですので、serviceへのつなぎこみがうまく行っていない気がします。

WorkingDirectory=/home/ubuntu/opt/csirt/
(csirtを一つ減らす)
だとうまくいくでしょうか。

投稿2019/04/07 07:06

ryotax

総合スコア142

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問