8
8
891099838382015-12-28 04:16:19
Nginx
89109983838, 2015-12-28 04:16:19

Flask-sqlalchemy instead of An error occurred?

Good time of the day!
I use a bunch of Freebsd 10, python34, nginx, uwsgi, supervisor, Flask
Faced such a problem:
In a python Web application on Flask EXACTLY when querying the database , for the type:

app.route('/setting/')
def setting():
    test = db.session.query(Setting).filter_by(group='setting').all()
    return redirect(url_for('index'), test = test)

Gives an error in browsers:
An error occurred.
Sorry, the page you are looking for is currently unavailable.

And in the nginx logs:
2015/12/28 06:53:47 [error] 527#0: *66 upstream prematurely closed connection while reading response header from upstream, client: 192.168.137.1, server: 192.168.137.2, request: "GET /root/setting / HTTP/1.1", upstream: "uwsgi://127.0.0.1:4242", host: "192.168.137.2"

Although when running through the built-in Flask server on port 81, everything works fine, without the above errors !!!
app config:
import sys, os
# configuration
SRF_ENABLED = True
SECRET_KEY = '1234'
DEBUG = 'False'
DEBUG_TB_INTERCEPT_REDIRECTS = False
# database
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
SQLALCHEMY_ECHO = False
SQLALCHEMY_TRACK_MODIFICATIONS = False

nginx config:
user  www;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    proxy_buffers 8 32k;
    proxy_buffer_size 64k;

    #access_log  logs/access.log  main;

    sendfile        on;
    client_max_body_size 20M;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
  
    listen       80;
    server_name  192.168.137.2;

    location / { try_files $uri @app; }
    location @app {
      uwsgi_pass 127.0.0.1:4242;
      include uwsgi_params;
      uwsgi_buffer_size 32k;   # Менял на значения 64 не помогло!!!
      uwsgi_buffers 8 32k;      # Менял на значения 64 не помогло!!!
      uwsgi_busy_buffers_size 32k;   # Менял на значения 64 не помогло!!!
    }

    error_page   500 502 503 504  /50x.html;
      location = /50x.html {
        root   /usr/local/www/nginx-dist;
    }
  }		
}

What could be the reason!
upstream prematurely closed connection while reading response header from upstream --- already studied, most likely not connected with this. Moreover , this error only slips out on queries to the database and only in conjunction with ngnix . I will add something else with an empty database and with records - all the same glitches !!
D
Tell me what's the problem with sqlalchemy?!
#################### SOLUTION ########################
The answer was quite simple:
uwsgi - runs as www, and the database was created as a different user user!
accordingly: Flask did not have the rights to work with the database and was buggy - chopping off from uwsgi, but on the DEV server everything worked, of course. the rights there are the same, let's say so!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
8
89109983838, 2015-12-29
@89109983838

In general, I configured uwsgi to work at 0.0.0.0:8000 (by adding protocol=http)
In the browser, as before, only those pages that are generated without accessing the Database are opened!
And pages with an image to the base (by type as an example above) gives
While the page was loading, the connection to the server was reset.
The answer turned out to be quite simple:
uwsgi - runs as www, and the database was created as a different user user!
accordingly: Flask did not have the rights to work with the database and was buggy - chopping off from uwsgi, but on the DEV server everything worked, of course. the rights there are the same, let's say so!
it is necessary to fasten logging to this place, maybe something will float!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question