【问题标题】:Getting 502 Bad Gateway error and sending a email with django, nginx, gunicorn收到 502 Bad Gateway 错误并使用 django、nginx、gunicorn 发送电子邮件
【发布时间】:2014-05-27 00:01:10
【问题描述】:

我正在尝试在我的网络服务器上使用 django send_mail 函数 gmail SMTP 发送电子邮件,但我收到了 502 Bad Gateway 错误。

我正在使用 nginx 和 gunicorn。

这是我的错误日志:

2014/04/12 16:46:55 [error] 26846#0: *11 upstream prematurely closed connection while                
reading response header from upstream, client: 179.162.163.62, server: example.com, 
request: "POST /contact/ HTTP/1.1", upstream: "http://127.0.0.1:9000/contact/", host: 
"example.com", referrer: "http://example.com/contact/"  

Ngnix 文件:

upstream example_gunicorn {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80;
    client_max_body_size 4G;
    server_name .example.com;

    keepalive_timeout 5;

    location /static/ {
        alias /deploy/sites/example/static/; # STATIC_ROOT
        expires 30d;
    }

    location /media/ {
        alias /deploy/sites/example/media/; # MEDIA_ROOT
        expires 30d;
    }

    location / {
        # checks for static file, if not found proxy to app
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass   http://example_gunicorn;
    }
}

【问题讨论】:

  • 检查 django 错误日志

标签: python django nginx gunicorn supervisord


【解决方案1】:

502 并没有说明实际的错误是什么……我给你画个图:

Client/Browser              Server                   Web Server               
    |         request         |                           |
    |------------------------>|                           |
    |                         |  attempt to send mail     |
    |                         |-------------------------> |
    |                         |                         ERROR 
    |                         |       error message       |          
    |                         |<--------------------------|
    |       502               |                           |
    |<----------------------- |                           |

箭头代表沟通。随着图像向下移动,时间会增加。

所以网络服务器对最终用户隐藏了实际错误,这在生产环境中是非常明智的行为。您需要查看网络服务器日志。

【讨论】:

    【解决方案2】:

    我在 DigitalOcean 上使用 CentOS7 液滴时遇到了类似的问题。我正在使用 Django 1.6.5 和 gunicorn。

    我通过在settings.py 中明确指定EMAIL_HOST = "74.125.22.108" 而不是EMAIL_HOST = "smtp.gmail.com" 解决了这个问题。就我而言,这是一个 IPv6 问题,如 DigitalOcean does not allow SMTP over IPv6。

    注意:为了安全起见,在盲目地将其粘贴到代码中之前,请始终验证 smtp.gmail.com 的 IPv4 地址(使用 dig 或类似地址)。

    【讨论】:

      【解决方案3】:

      尝试编辑您的 /etc/hosts

      127.0.0.1 localhost.localdomain localhost example.com
      127.0.0.1 localhost
      

      在服务器配置中增加您的proxy_read_timeout 60s; 也很有用。默认情况下它是 60 秒,但您可以将其设为 360 秒或其他。 并且不要忘记重新启动服务(网络、nginx、postfix 等)

      【讨论】:

        猜你喜欢
        • 2023-03-20
        • 2016-04-20
        • 2020-06-12
        • 2018-08-03
        • 2016-01-04
        • 2014-04-30
        • 2019-09-05
        • 2016-03-26
        • 2019-01-22
        相关资源
        最近更新 更多