nginx [=10th=] 404 如果我使用 django 发送电子邮件

nginx return 404 if I send e-mail using django

我正在尝试使用简单的联系方式发送电子邮件。 在本地开发中它有效,但在生产中我收到 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 配置

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

server {
    listen 80;
    server_name  www.example.com;
    rewrite ^/(.*) http://example.com/ 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/"

好的,这很难解决。

我不得不编辑 /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