111连接在文件上传时被拒绝-Flask / Nginx

时间:2018-10-04 13:50:08

标签: nginx flask web-applications gunicorn

正在寻找我无法解决的问题的一些见解,因为我对服务器的工作经验不足。

主要问题:如何查看更详细的错误信息?

我有一个Web应用程序,允许用户上传CSV文件;此csv已更改并作为下载返回给用户。就目前而言,此webapp在我的localhost开发环境中运行良好。当我尝试在单独的服务器上使用它时,上传文件时遇到了问题。我收到500内部服务器错误(111连接被拒绝)。

由于我没有经验,所以除了检查/var/log/myapp-error.log中的错误日志外,我不知道如何诊断此错误

2018/10/04 09:24:53 [error] 24401#0: *286 connect() failed (111: Connection refused) while connecting to upstream, client: 128.172.245.174, server: _, request: "GET / HTTP/1.1", upstream: "http://[::1]:8000/", host: "ip addr here"

我想知道如何了解导致此错误的原因。我已将Flask设置为调试模式,但也无法对此进行堆栈跟踪。如果有帮助,我将在下面放置一些flask应用程序和nginx配置。

@app.route('/<path:filename>')
def get_download(filename):
    return send_from_directory(UPLOAD_FOLDER, filename, as_attachment=True)

@app.route('/', methods = ['GET', 'POST'])
@app.route('/index', methods = ['GET', 'POST'])
def index():
    path = os.path.join(dirname(realpath(__file__)), 'uploads')
    uploaded = ''

    if request.method == 'POST':
            if 'file' not in request.files:
                    flash('no file part')
                    return redirect(request.url)
            file = request.files['file']

            if file.filename == '':
                    flash('no selected file')
                    return redirect(request.url)

            if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    uploaded = str(path + '/' + filename)

                    df = preprocess_table(uploaded)

                    fp = classify(df, filename)
                    print(fp)
                    head, tail = os.path.split(fp)
                    print(tail)
                    get_download(tail)

                    return send_from_directory(UPLOAD_FOLDER, tail, as_attachment=True)

    return render_template('index.html')

这是我的Nginx设置。

server {
    #listen on port 80 http
    listen 80;
    server_name _;
    location / {
            #redirect requests to same URL but https
            return 301 https://$host$request_uri;
    }
}

server {
    #listen on port 443 https
    listen 443 ssl;
    server_name _;

    #location of the self sign ssl cert
    ssl_certificate /path/to/certs/cert.pem;
    ssl_certificate_key /path/to/certs/key.pem;

    #write access and error logs to /var/log
    access_log /var/log/myapp_access.log;
    error_log /var/log/myapp_error.log;

    location / {
            #forward application requests to the gunicorn server
            proxy_pass http://localhost:8000;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

    location /static {
            #handle static files directly, without forwarding to the application
            alias /path/to/static;
            expires 30d;
    }
}

赞赏任何见解/建议。谢谢!

0 个答案:

没有答案