[英]Getting 502 Bad Gateway error and sending a email with django, nginx, gunicorn
我试图在Web服务器上使用django send_mail函数gmail SMTP发送电子邮件,但出现502 Bad Gateway错误。
我正在使用Nginx和Gunicorn。
这是我的error.log:
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;
}
}
502并没有过多说明实际错误是什么...我给你画一张照片:
Client/Browser Server Web Server
| request | |
|------------------------>| |
| | attempt to send mail |
| |-------------------------> |
| | ERROR
| | error message |
| |<--------------------------|
| 502 | |
|<----------------------- | |
箭头表示通信。 时间随着向下移动图像而增加。
因此,Web服务器向最终用户隐藏了实际错误,这在生产环境中是非常明智的行为。 您需要查看Web服务器日志。
我在DigitalOcean上使用CentOS7小滴时遇到了类似的问题。 我正在使用Django 1.6.5和gunicorn。
我通过在settings.py
显式指定EMAIL_HOST = "74.125.22.108"
而不是EMAIL_HOST = "smtp.gmail.com"
来解决此问题。 就我而言,这是一个IPv6问题,因为DigitalOcean 不允许 SMTP over IPv6。
注意:为了安全起见,请始终先验证smtp.gmail.com的IPv4地址(使用dig
或类似方法),然后再将其盲目粘贴到代码中。
尝试编辑您的/ etc / hosts
127.0.0.1 localhost.localdomain localhost example.com
127.0.0.1 localhost
也可能会增加您的proxy_read_timeout 60s;
在服务器配置中。 默认情况下为60,但您可以将其设置为360。 并且不要忘记重启服务(网络,nginx,后缀等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.