django - NGINX 502 错误网关 gunicorn 超时

标签 django nginx gunicorn

将数据保存到数据库时弹出 502 bad gateway 错误。

一旦用户登录到 django 应用程序 (OLE 7)。从 ldap 服务器中提取有关该用户的数据并保存在我的本地数据库(postgres)中。配置 nginx、gunicorn 后,它在本地服务器上运行良好,一旦用户登录网站而不是显示检索到的数据,它会显示 502 Bad gateway。我浏览了很多关于这个的 stackoverflow 帖子,有人说增加超时,检查 gunicorn 是否正在运行。我已经尝试了所有这些,但仍然无法正常工作。

nginx.conf

    user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
worker_connections  1024;
}
er  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
  worker_connections  1024;
}


http {
include       /etc/nginx/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  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  300;

#gzip  on;
upstream app_server {
    server 10.111.xxx.xxx:8001 fail_timeout=0;
}

server{
 listen 80;
 server_name 10.111.xxx.xxx;
 location = /favicon.ico { access_log off; log_not_found off; }
 location /static/ {root /home/lisa/revcon;}
 location / {
    proxy_set_header Host $http_host;
    proxy_set_header Connection "";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://unix:/home/lisa/revcon/revcon.sock;
    proxy_connect_timeout 500s;
    proxy_read_timeout 600s;
}

views.py

 def save_information(request):

associate_id = id_to_numeric(request.user.username)
ldap = Ldap()

associate_details = ldap.search(associate_id=associate_id)[0]

details = UserDetails(
    associate_name=associate_details['name'],
    associate_nbr=associate_id,
    associate_email=associate_details['email'],
   associate_department_id=id_to_numeric(associate_details['department']),
    associate_mgr=associate_details['managerCN'],
    associate_exec=associate_details['execCN'],
    associate_org=associate_details['org'],
    associate_image=img,
    date_of_service=rcm_date,
    title=associate_details['title'],
    client=client,
    lob=lob,
    phone_number=associate_details['mobile'],
)
details.save()
messages.success(request, 'Profile created! ')
return redirect('/?submit=true')

nginx 错误日志如下所示:

2019/05/15 04:01:55 [error] 7602#7602: *1 upstream prematurely closed connection while reading response header from upstream, client: 10.111.044.xxx, server: 10.111.xxx.xxx, request: "POST /save_information HTTP/1.1", upstream: "http://unix:/home/lisa/revcon/revcon.sock:/save_information", host: "10.111.xxx.xxx", referrer: "http://10.111.xxx.xxx/pr/"

unicorn 从“django_session”中选择“django_session”。“session_key”,“django_session”,“session_data”,“django_session”。 expire_date"> '2019-05-15T09:04:53.974657+00:00'::timestamptz); args=('hzy3dsjxfzqxhds6kd5uyteux0gps9d1', datetime.datetime(2019, 5, 15, 9, 4, 53, 974657, tzinfo=)) (0.001) 选择“auth_user”。 ", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined"FROM "auth_user"WHERE "auth_user". “编号”= 1;参数=(1,) (0.001) SELECT (1) AS "a"FROM "CollectData_userdetails"WHERE "CollectData_userdetails"."associate_nbr"= '050667' LIMIT 1; args=(u'050667',) 解析模板“CreateUser.html”中的变量“img”时出现异常。 追溯(最近一次通话): 文件“/home/lisa/revcon/env/lib/python2.7/site-packages/django/template/base.py”,第 903 行,在 _resolve_lookup (bit, current)) # 缺失的属性 VariableDoesNotExist:在 u'[{\'False\': False,\'None\': None,\'True\': True}, {u\'csrf_token\': ,\' 中查找键 [img] 失败user\': >,\'perms\': ,\'DEFAULT_MESSAGE_LEVELS\': {\'DEBUG\': 10,\'INFO\': 20,\'WARNING\': 30,\'SUCCESS\': 25,\'ERROR\': 40},\'messages\': , u\'request\': }, {}, {\'form\': }, {\'block\':\n, ,\n\n\n\n, , , , 关联\'>, , , , , , , , , ,\n\n\t\t\t , ,\\n\\t\\t\\t , ,\\n]>}]' 解析模板“bootstrap4/field.html”中的变量“标签”时出现异常。

它点击了这个 url url('save_information', views.save_information, name='save_information'), 然后失败。 502 错误。

最佳答案

我想通了。在我服务的/etc/systemd/system/gunicorn 中的 gunicorn.service 文件中。 我做了一个改变:增加了超时时间。 gunicorn 的默认超时时间为 30 秒,因此我更新为 120 秒。

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=lisa
Group=nginx
Restart=on-failure
WorkingDirectory=/home/lisa/revcon
ExecStart=/home/lisa/revcon/env/bin/gunicorn --**timeout 120** --workers 5 
--bind unix:/home/lisa/revcon/env.sock revcon.wsgi:application

[Install]
WantedBy=multi-user.target
~

它对我来说效果很好。

关于django - NGINX 502 错误网关 gunicorn 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56161700/

相关文章:

python - 由于 KafkaTimeoutError,无法使用 kafka-python 从 django 应用程序向 kafka 发送消息

python - Django 1.8 : ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class

node.js - 我的 Node.js 应用程序在将其放在 nginx 后面后,在写入请求时随机收到未处理的 'error' 事件

ssl - 如何使用 SSL 在 Kubernetes 中配置 Flask ws?

python - Django + fcgi = 未显示对 views.py 的新编辑

python - 错误 : book. 书.作者 : (fields. E300) 书.书.作者 : (fields. E307)

java - Elastic Beanstalk .ebextensions 在 WAR 中被忽略

ssl - nginx ldap模块错误

python - 使用 Gunicorn 和 Gevent 运行 Flask 时使用请求发出非阻塞请求

python - 防止慢速查询耗尽 gunicorn 工作池