我正在尝试安装新服务器,并且无法使用nginx和uwsgi运行Django。我收到错误消息“ 502 Bad Gateway”,错误日志中有我不理解的消息:
2019/07/20 10:50:44 [error] 2590#2590: *1 upstream prematurely closed connection while reading response header from upstream, client: 79.183.208.33, server: *.speedy.net.3.speedy-technologies.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/run/uwsgi/app/speedy_net/socket:", host: "3.speedy-technologies.com"
我有4个网站,这是主要(默认)配置文件:
server {
listen [::]:80 default_server;
listen 80 default_server;
server_name *.speedy.net.3.speedy-technologies.com speedy.net.3.speedy-technologies.com;
access_log /var/log/nginx/speedy-net.access.log;
error_log /var/log/nginx/speedy-net.error.log;
client_max_body_size 50M;
root /home/ubuntu/speedy-net/speedy/net/static_serve/root;
try_files $uri @uwsgi;
location @uwsgi {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/app/speedy_net/socket;
}
location /admin/ {
auth_basic "admin site";
auth_basic_user_file /etc/nginx/htpasswd_admin;
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/app/speedy_net/socket;
}
location /static {
alias /home/ubuntu/speedy-net/speedy/net/static_serve;
access_log off;
# expires max;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_vary on;
gzip_comp_level 6;
}
}
并且:
[uwsgi]
project = net
chdir = /home/ubuntu/speedy-net
home = %(chdir)/env
module = speedy.%(project).wsgi:application
plugins = python3
master = true
processes = 4
chmod-socket = 666
vacuum = true
uid = ubuntu
gid = ubuntu
touch-reload = /run/uwsgi/app/speedy_%(project)/reload
我尝试使用sudo nginx -t
进行测试,但是收到一条消息,表明我的配置可以。有什么问题吗?
编辑:
旧服务器(正在运行):
$ find /run/uwsgi/ -ls
489 0 drwxr-xr-x 3 root root 60 Jul 20 08:41 /run/uwsgi/
490 0 drwxr-xr-x 6 root root 120 Jul 20 08:41 /run/uwsgi/app
500 0 drwxr-xr-x 2 ubuntu ubuntu 100 Jul 20 08:42 /run/uwsgi/app/speedy_net
523 0 -rw-rw-r-- 1 ubuntu ubuntu 0 Jul 20 08:43 /run/uwsgi/app/speedy_net/reload
501 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 08:41 /run/uwsgi/app/speedy_net/socket
457 4 -rw-r--r-- 1 root root 5 Jul 20 08:41 /run/uwsgi/app/speedy_net/pid
498 0 drwxr-xr-x 2 ubuntu ubuntu 100 Jul 20 08:42 /run/uwsgi/app/speedy_match
524 0 -rw-rw-r-- 1 ubuntu ubuntu 0 Jul 20 08:43 /run/uwsgi/app/speedy_match/reload
499 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 08:41 /run/uwsgi/app/speedy_match/socket
413 4 -rw-r--r-- 1 root root 5 Jul 20 08:41 /run/uwsgi/app/speedy_match/pid
495 0 drwxr-xr-x 2 ubuntu ubuntu 100 Jul 20 08:43 /run/uwsgi/app/speedy_mail
528 0 -rw-rw-r-- 1 ubuntu ubuntu 0 Jul 20 08:43 /run/uwsgi/app/speedy_mail/reload
497 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 08:41 /run/uwsgi/app/speedy_mail/socket
496 4 -rw-r--r-- 1 root root 5 Jul 20 08:41 /run/uwsgi/app/speedy_mail/pid
491 0 drwxr-xr-x 2 ubuntu ubuntu 100 Jul 20 08:42 /run/uwsgi/app/speedy_composer
527 0 -rw-rw-r-- 1 ubuntu ubuntu 0 Jul 20 08:43 /run/uwsgi/app/speedy_composer/reload
494 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 08:41 /run/uwsgi/app/speedy_composer/socket
493 4 -rw-r--r-- 1 root root 5 Jul 20 08:41 /run/uwsgi/app/speedy_composer/pid
新服务器:
$ find /run/uwsgi/ -ls
554 0 drwxr-xr-x 3 root root 60 Jul 20 10:15 /run/uwsgi/
555 0 drwxr-xr-x 6 root root 120 Jul 20 11:14 /run/uwsgi/app
573 0 drwxr-xr-x 2 ubuntu ubuntu 80 Jul 20 11:14 /run/uwsgi/app/speedy_net
598 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 11:14 /run/uwsgi/app/speedy_net/socket
584 4 -rw-r--r-- 1 root root 5 Jul 20 11:14 /run/uwsgi/app/speedy_net/pid
568 0 drwxr-xr-x 2 ubuntu ubuntu 80 Jul 20 11:14 /run/uwsgi/app/speedy_match
572 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 11:14 /run/uwsgi/app/speedy_match/socket
569 4 -rw-r--r-- 1 root root 5 Jul 20 11:14 /run/uwsgi/app/speedy_match/pid
565 0 drwxr-xr-x 2 ubuntu ubuntu 80 Jul 20 11:14 /run/uwsgi/app/speedy_mail
567 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 11:14 /run/uwsgi/app/speedy_mail/socket
566 4 -rw-r--r-- 1 root root 5 Jul 20 11:14 /run/uwsgi/app/speedy_mail/pid
499 0 drwxr-xr-x 2 ubuntu ubuntu 80 Jul 20 11:14 /run/uwsgi/app/speedy_composer
564 0 srw-rw-rw- 1 ubuntu ubuntu 0 Jul 20 11:14 /run/uwsgi/app/speedy_composer/socket
561 4 -rw-r--r-- 1 root root 5 Jul 20 11:14 /run/uwsgi/app/speedy_composer/pid
旧服务器为Ubuntu 16.04.6 LTS
,新服务器为Ubuntu 18.04.2 LTS
。
答案 0 :(得分:2)
可能存在多个问题,您需要进行调试,看看有什么问题
1。未设置ALLOWED_HOSTS
如果您的Django配置未设置ALLOWED_HOSTS
,则可能会产生错误
2。 nginx config缺少某些指令
有些人可以使用下面的方法解决问题
sendfile on;
client_max_body_size 20M;
keepalive_timeout 0;
large_client_header_buffers 8 32k;
查看将其添加到您的路线是否有帮助
3。 Django应用因某些未处理的异常而崩溃
这需要您查看uwsgi
日志。您可以通过在一个终端上运行uwsgi
进程而不是将其作为服务来执行此操作,或者在uwsgi.ini
[uwsgi]
logto=/var/log/uwsgi.log
log-ioerror=1
然后查看日志以了解导致崩溃的原因
4。您的请求花了时间,而且超时了
在这种情况下,您需要增加uwsgi
的超时时间。您可以在下方添加到uwsgi.ini
[uwsgi]
harakiri = 120
及以下到nginx配置
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
如果您查看共享的日志
/usr/bin/uwsgi(uwsgi_segfault+0x23) [0x563d84f50d73]
/lib/x86_64-linux-gnu/libc.so.6(+0x3ef20) [0x7f4d7ff98f20]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/./libssl-c0c2ede4.so.1.0.2q(ssl3_cleanup_key_block+0xb) [0x7f4d773d4cab]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/./libssl-c0c2ede4.so.1.0.2q(ssl3_clear+0x16) [0x7f4d773d25a6]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/./libssl-c0c2ede4.so.1.0.2q(tls1_clear+0x9) [0x7f4d773de219]
/usr/lib/x86_64-linux-gnu/libssl.so.1.1(SSL_new+0x3f2) [0x7f4d80c11a22]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/libpq-bd31fe2b.so.5.11(+0x23969) [0x7f4d77669969]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/libpq-bd31fe2b.so.5.11(+0x24fc5) [0x7f4d7766afc5]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/libpq-bd31fe2b.so.5.11(PQconnectPoll+0xb78) [0x7f4d77654ba8]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/libpq-bd31fe2b.so.5.11(+0xfa28) [0x7f4d77655a28]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/.libs/libpq-bd31fe2b.so.5.11(PQconnectdb+0x1f) [0x7f4d7765841f]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/_psycopg.cpython-36m-x86_64-linux-gnu.so(+0x12b01) [0x7f4d778a9b01]
/home/ubuntu/speedy-net/env/lib/python3.6/site-packages/psycopg2/_psycopg.cpython-36m-x86_64-linux-gnu.so(+0x1388f) [0x7f4d778aa88f]
/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(+0x122342) [0x7f4d7c220342]
这些表明,由于Postgres DB连接和ssl而导致崩溃。现在有很多话题讨论不同的解决方案,可以帮助您解决问题
uwsgi segmentation fault when serving a django application
django app under uwsgi failing to start when using postgresql database
Error importing psycopg2._psycopg when using uWSGI
uwsgi segmentation fault for one specific route
参考
https://serverfault.com/questions/276462/nginx-uwsgi-gives-502-bad-gateway
uWSGI request timeout in Python
upstream prematurely closed connection (uwsgi + nginx + django)
uwsgi + nginx + flask: upstream prematurely closed
nginx, uwsgi, DJango, 502 when DEBUG=False, "upstream prematurely closed connection"
答案 1 :(得分:0)
尝试将套接字指令添加到uwsgi配置文件中。
socket = /run/uwsgi/app/speedy_%(project)/socket
您必须告诉uwsgi在哪里可以找到套接字。也许在旧服务器上,套接字路径是作为命令行标志传递的?
nginx用户和uswgi用户都应具有对套接字本身的rw
权限和对每个父目录的rx
权限。您可以通过以相应用户身份运行touch
来检查两个用户都可以写入套接字。
sudo --user=nginx touch /run/uwsgi/app/speedy_net/socket
sudo --user=uwsgi touch /run/uwsgi/app/speedy_net/socket
如果其中任何一个失败,则表示权限问题。
答案 2 :(得分:0)
最终它是https://stackoverflow.com/a/54253374/57952。我必须运行以下2条命令:
/home/ubuntu/speedy-net/env/bin/pip uninstall psycopg2
/home/ubuntu/speedy-net/env/bin/pip install --no-binary :all: psycopg2==2.7.7
然后服务器开始工作。