W
W
Wolf47892020-12-13 21:35:39
Django
Wolf4789, 2020-12-13 21:35:39

What to do with the error when submitting the form "upstream closed prematurely"?

The nginx + django + ajax bundle is configured to send the contact form.
Suddenly, an error began to occur when submitting a form on the site. The form does not come, there
is an error in the logs - upstream prematurely closed connection while reading response header from upstream.

Ajax:

$('#send_form').submit(function (e) {
    e.preventDefault();
    $.ajax({
        type: 'get',
        url: '/send',
        data: $(this).serialize(),
        success: function () {
          alert('form was submitted');
        }
      });
});


Django:
from django.views.generic import View
from django.http import HttpResponse
from django.core.mail import send_mail
from django.template.loader import render_to_string

from django.conf import settings


class Send(View):
    def get(self, request, *args, **kwargs):
        print(request.GET)
        message = render_to_string('email.html', {'data': request.GET})
        send_mail(
            'New message on site xxxxx.ru,
            message,
            settings.DEFAULT_FROM_EMAIL,
            [settings.EMAIL_DEFAULT_TO],
            fail_silently=False,
        )
        return HttpResponse(status=200)


Nginx:
events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   150;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;

    server {
  listen 80;
  server_name xxxxx.ru;
  
  location /static/ {
    root /var/www/xxxxxxxxx;
    index index.html index.htm index.php;
  }

  location / {
    root /var/www/xxxxxxxx;
    proxy_pass http://127.0.0.1:8888;
    index index.html index.htm index.php;
  }
  }
  
   server {
  listen 80;
  server_name xxxxxxx;
  
  location /static/ {
    root /var/www/xxxxxxx;
    index index.html index.htm index.php;
  }

  location / {
    root /var/www/xxxxxxx;
    proxy_pass http://127.0.0.1:8888;
    index index.html index.htm index.php;
  }
    }


The form:
<form id='send_form'>
     <input type="text" name='name' placeholder="Name" required>
     <input type="email" name='email' placeholder="E-mail" required>
     <textarea name="message" id="" cols="30" rows="10" placeholder="Message"></textarea>
     <input name='submit' type="submit" value='Send'>
   </form>


I didn’t seriously change anything, I didn’t add scripts, I don’t understand what’s wrong.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question