当django app发布形式并尝试发送(gmail)电子邮件时,Nginx失败

时间:2017-07-27 16:02:25

标签: django nginx

在我的Django + nginx应用程序中,自从我实现了一个发送(gmail)电子邮件的联系表单后,我遇到了问题。

<form method='POST' action=''>{% csrf_token %}
   {{ form|crispy}}
   <button class="btn btn-xl" type="submit">Send Message</button>
</form>

处理表格POST的视图:

if request.method == "POST":
    form = ContactsForm(request.POST or None)
    if form.is_valid():
        form_nome = form.cleaned_data['nome']
        form_email = form.cleaned_data['email']
        telefono = form.cleaned_data['telefono']
        form_message = form.cleaned_data['message']
        from_email = settings.EMAIL_HOST_USER
        to_email = [from_email]
        subject = "test"
        contact_message = "%s: %s via %s"%(
            form_nome,
            form_message,
            form_email
            )
        send_mail(
            subject,
            contact_message,
            from_email,
            to_email,
            fail_silently=False)
        return HttpResponseRedirect('#contact')
else:
    form = ContactsForm()

我读了很多关于我的问题的q&amp; a但没有人适合我的问题。

在服务器中:

uwsgi.ini

[uwsgi]
module=core.wsgi:application
socket=/tmp/uwsgi_portfoliomga.sock
master=True
pidfile=/tmp/project-master_portfoliomga.pid
processes=4
threads=10
vacuum=True
max-requests=5000
harakiri=30
daemonize=/home/martina/www/portfoliomga/logs/portfoliomga.log
stats=/tmp/stats_portfoliomga.sock

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

的nginx / error.log中

[error] 16503#16503: *107 upstream prematurely closed connection while reading response header from upstream, client: 79.16.225.200, server: <myAPPaddress>.it, request: "POST / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi_portfoliomga.sock:", host: "<myAPPaddress>", referrer: "http://<myAPPaddress>.it/"

任何人都可以分享光明吗?

提前谢谢

修改 / etc / nginx / sites-available /&lt;(myAPPaddress)&gt;配置文件

server {
  listen 80;
  client_max_body_size 5M;
  server_name <myAPPaddress>;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  if (-f /home/martina/www/portfoliomga/releases/current/.maintenance) {
    return 503;
  }

  if (!-d /home/martina/www/portfoliomga/releases/current) {
    return 503;
  }

  error_page 503 @maintenance;
  location @maintenance {
    root    /home/martina/www/portfoliomga/htdocs;
    rewrite ^(.*)$ /maintenance.html break;
  }

  location /static {
    root /home/martina/www/portfoliomga;
  }
  location /media {
    root /home/martina/www/portfoliomga;
  }
  location /robots.txt {
    root /home/martina/www/portfoliomga/htdocs/robots.txt;
  }
  location /sitemap.xml {
    root /home/martina/www/portfoliomga/htdocs/sitemap.xml.txt;
  }
  location / {
    uwsgi_pass unix:/tmp/uwsgi_portfoliomga.sock;
    include /etc/nginx/uwsgi_params;
  }
}

1 个答案:

答案 0 :(得分:0)

Nginx抱怨是因为我在没有邮件服务器的情况下拍摄电子邮件。纽比: - )

我安装了postfix,经过痛苦的配置后,我的webapp现在终于可以直接从我的Gmail帐户发送电子邮件了。

相关问题