如果我使用django发送电子邮件,nginx返回404

时间:2015-03-21 17:58:14

标签: django email nginx http-status-code-404

我正在尝试使用简单的联系表单发送电子邮件。 在本地开发它可以工作,但在生产时我得到404错误,没有发送电子邮件。

我拥有的: Nginx,Gunicorn,Django

settings.py

DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['example.com', 'www.example.com']

EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'googleapppassword'
EMAIL_USE_TLS = True

的access_log

"POST /contact/ HTTP/1.1" 404 198 "http://example.com/contact/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

更新:

nginx config

upstream example.com {
    server localhost:12345 fail_timeout=0;
}

server {
    listen 80;
    server_name  www.example.com;
    rewrite ^/(.*) http://example.com/$1 permanent;
}

server {
    listen 80;
    client_max_body_size 4G;
    server_name example.com;
    access_log  /path/to/access.log;
    keepalive_timeout 5;

    root /path/to/static_content;

    location ~ ^/(static|media)/ {
        access_log off;
        expires 60d;
        add_header Cache-Control public;
        }

    location / {
        proxy_pass http://example.com;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /path/to/static_content/static/html;
    }

    location ~ ^/(static|media)/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://example.com;
            break;
        }
     }
}

error.log中

2015/03/21 15:36:04 [error] 28486#0: *272 upstream prematurely closed connection while reading response header from upstream, client: 95.65.125.145, server: example.com, request: "POST /contact/ HTTP/1.1", upstream: "http://127.0.0.1:12345/contact/", host: "example.com", referrer: "http://example.com/contact/"

1 个答案:

答案 0 :(得分:0)

好的,这很难解决。

我必须编辑/ etc / hosts文件并重新启动服务

127.0.0.1 localhost.localdomain localhost example.com
127.0.0.1 localhost
127.0.1.1 example.com 

此后我重启了服务

$ sudo /etc/init.d/networking restart
$ sudo service nginx restart
$ sudo service postfix restart

现在它的超高速工作没有任何错误。

P.S。 postfix在列表中,因为我试图通过使用postfix而不是smtp.gmail.com来解决问题,但不需要postfix。 另外,请查看/ etc / hostname和/ etc / mailname