NGINX 502 坏网关 gunicorn 超时

Posted

技术标签:

【中文标题】NGINX 502 坏网关 gunicorn 超时【英文标题】:NGINX 502 bad gateway gunicorn timeout 【发布时间】:2019-10-03 07:51:18 【问题描述】:

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

一旦用户登录到 django 应用程序 (OLE 7)。从 ldap 服务器中提取有关该用户的数据并保存在我的本地数据库(postgres)中。配置 nginx 后,它在本地服务器上运行得非常好,一旦用户登录网站而不是显示检索到的数据,它就会显示 502 Bad gateway。我浏览了很多关于此的 *** 帖子,有人说增加超时,检查 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 [错误] 7602#7602: *1 上游提前关闭连接,同时从上游读取响应标头,客户端:10.111.044.xxx,服务器:10.111.xxx.xxx,请求:“POST /save_information HTTP/1.1”,上游:“http://unix:/home/lisa/revcon/revcon.sock:/save_information”,主机:“10.111.xxx.xxx”,引用者:“http://10.111.xxx.xxx/pr/”

独角兽 SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = 'hzy3dsjxfzqxhds6kd5uyteux0gps9d1' AND "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) SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "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"."id" = 1; args=(1,) (0.001) SELECT (1) AS "a" FROM "CollectData_userdetails" WHERE "CollectData_userdetails"."associate_nbr" = '050667' LIMIT 1; args=(u'050667',) Exception while resolving variable 'img' in template 'CreateUser.html'. Traceback (most recent call last): File "/home/lisa/revcon/env/lib/python2.7/site-packages/django/template/base.py", line 903, in _resolve_lookup (bit, current)) # missing attribute VariableDoesNotExist: Failed lookup for key [img] in u'[\'False\': False, \'None\': None, \'True\': True, u\'csrf_token\': , \'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, , , , Associate \'>, , , , , , , , , , \n\n\t\t\t , , \\n\\t\\t\\t , , \\n]>]' Exception while resolving variable 'tag' in template 'bootstrap4/field.html'.

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

【问题讨论】:

能提供gunicorn的日志吗? 我已经更新了问题,在那里添加了 gunicorn 日志。 【参考方案1】:

我想通了。在 /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
~

它对我来说效果很好。

【讨论】:

【参考方案2】:

错误 502,gunicorn 服务超时

[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/projects/myproject/project
ExecStart=/home/user/projects/myproject/projectenv/bin/gunicorn\
                --access-logfile - \
                --workers 3 \
                --timeout 1000 \ # this
                --bind unix:/run/gunicorn.sock \
                project.wsgi:application

504 附近的错误是 nginx 服务器超时:

location /
                include proxy_params;
                proxy_read_timeout 1000; # this
                proxy_pass http://unix:/run/gunicorn.sock;
        

【讨论】:

以上是关于NGINX 502 坏网关 gunicorn 超时的主要内容,如果未能解决你的问题,请参考以下文章

django-gunicorn-nginx:502 网关错误

Nginx 502 坏网关节点 JS

502坏网关Elastic Beanstalk Spring Boot

nginx http 502 503 504区别 · T

502 错误 django + nginx + gunicorn - connect() recv()

Django + nginx + gunicorn 给出 502 错误。日志信息很少[关闭]