尝试上传文件时,Nginex + Flask上游过早关闭连接

时间:2019-12-27 16:46:28

标签: python flask

当我尝试使用python和flask通过我的Web api上传文件时遇到了一些问题,因此每当尝试这样做时,我都会收到以下错误消息(无论任何类型的文件,无论大小):

2019/12/18 16:30:04 [error] 111206#111206: *12251 upstream prematurely closed connection while reading response header from upstream, 
client: xxx.61.xxx.70, server: *mydomain.com*, request: "POST /upload HTTP/1.1", upstream: "uwsgi://unix:/home/climpia/tracking/tracking.sock:", host: "*api.mydomain.com*", referrer: "https://client.mydomain.com/?id_ruta=14130"

我不确定问题出在哪里,因为我已经在uWSGI.ini和nginex.config文件上调整了一些选项,但是我仍然收到相同的错误消息。我已经检查了代码,但不知道有什么问题。

这是uWSGI.ini:

[uwsgi]
module = wsgi

master = true
processes = 5

socket-timeout = 65

http-keepalive = 256

socket = tracking.sock
chmod-socket = 660
vacuum = true

#location of log files
logto = /tmp/%n.log

die-on-term = true

这是此项目的nginex.config文件:

server {
    server_name mydomain.com api.mydomain.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/tracking/tracking.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    sendfile        on;
    client_max_body_size 512M;
    keepalive_timeout  0;
}
server {
    if ($host = api.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name mydomain.com api.mydomain.com;
    return 404; # managed by Certbot
}

这是python代码:

# tracking.py

from flask import Flask, request, session, g, redirect, \
    url_for, abort, render_template, flash, jsonify, \
    make_response, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text
from flask_cors import CORS
from datetime import datetime
from werkzeug.utils import secure_filename
import os

app = Flask(__name__, static_folder='static')
app.config.from_object(__name__)
cors = CORS(app, resources={r"/*": {"origins": "https://client.mydomain.com"}})
#cors = CORS(app, resources={r"/*": {"origins": "*"}})
app.config.from_envvar('APP_CONFIG_FILE', silent=True)

DB_URL = 'postgresql+psycopg2://user:pwsd@127.0.0.1/NAME DB'

UPLOAD_FOLDER = 'static/upload'
ALLOWED_EXTENSIONS = {'qpj', 'cpg', 'prj', 'dbf', 'shx', 'shp', 'txt', 'json'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
#app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # silence the deprecation warning
#app.config['SQLALCHEMY_ECHO'] = True
app.config['DEBUG'] = True

db = SQLAlchemy(app)

@app.route('/upload',methods = ['POST'])
def upload_file():
    response_object = {'status': 'no se ha cargado nada'}
    if request.method =='POST':
        if 'flecheo' not in request.files:
            print('no viene el FLECHEO')
        files = request.files['flecheo']
        for f in files:
            print(f.filename)
            filename = secure_filename(f.filename)
            app.logger.info('FileName: ' + filename)
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename))
            response_object['message'] = 'Subieron los archivos del shape file'

    return jsonify(response_object)

这是杰森:

function uploadFiles(){
  var capa = document.getElementsByName('capa'); 
  var flecheo = document.getElementById('getFiles').files;
  formData = new FormData();

  for(i = 0; i < capa.length; i++) { 
    if(capa[i].checked) 
      formData.append('capa', capa[i].value);
  } 

  for(i=0; i < flecheo.length; i++){
    console.log(flecheo[i]);
    formData.append('flecheo', flecheo[i]);
  }

  for (var key of formData.entries()) {
    console.log(key[0] + ', ' + key[1]);
  }

  var contentType = {
    headers: {
      'content-type': 'multipart/form-data'
    }
  };

  axios.post('https://api.mydomain.com/upload', formData, contentType)
    .then(response => {
      console.log(response);
    }).catch(error => {
      console.log(error);
    });
}

非常感谢您的帮助!

0 个答案:

没有答案