502 bad gateway nginx. uwsgi, flask

7,818

Two things I think helped to solve this PROPAGATE_EXCEPTIONS = True in config.py and I removed threading from my vassal ini file the ending uwsgi files looked like this:

/etc/uwsgi/emperor.ini:

[uwsgi]
emperor = /etc/uwsgi/vassals
master = true
plugins = python2
uid = http
gid = http

/etc/uwsgi/vassals/test.ini:

[uwsgi]
chdir = /srv/http/test_dir/src
wsgi-file = run.py
callable = app
processes = 4
stats =  127.0.0.1:9191
max-requests = 5000
enable-threads = true
vacuum = true
thunder-lock = true
socket = /run/uwsgi/test-sock.sock
chmod-socket = 664
harakiri = 60
logto = /var/log/uwsgi/test.log

Not sure on the PROPAGATE_EXCEPTIONS = True but removing the threads option in test.ini and making sure there was a master option in emperor.ini seemed to have solved the issue of sql being tossed around to different treads, or at least it complaining about it and crashing the site out, either or.

Share:
7,818

Related videos on Youtube

Never Nor
Author by

Never Nor

Updated on September 18, 2022

Comments

  • Never Nor
    Never Nor over 1 year

    Ok so I developed a web app in python and it worked with the internal server:

    if __name__ == '__main__':
        app.app.run(debug = True)
    

    with no issue at all. I am now trying to start up a nginx server with uwsgi running on arch linux, here are the config files:

    the config.py for my app:

    CSRF_ENABLED = True
    SECRET_KEY = "you'll-never-know"
    PROPAGATE_EXCEPTIONS = True
    
    import os
    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')
    

    /etc/nginx/nginx.conf:

    user http;
    worker_processes  2;
    
    events {
        #worker_connections  1024;
        worker_connections 2048;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  70;
    
        gzip  on;
        gzip_min_length  1100;
        gzip_buffers  4 32k;
        gzip_types    text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
        gzip_vary on;
        gzip_comp_level 6;
    
        include /etc/nginx/sites-enabled/*;
    }
    

    /etc/nginx/sites-enabled/test.conf

    upstream test-server {
        server unix:/run/uwsgi/test.sock;
        #server 127.0.0.1:3031
    }
    
    server {
        listen 80;
        server_name "";
        return 444;
    }
    
    server {
        server_name 192.xxx.x.xxx;
        listen 80;
    
        root /srv/http/test-dir/src/app;
    
        location /static {
            alias /srv/http/test-dir/src/app/static;
        }
    
        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass test-server;
        }
    
    #    rewrite ^ https://$server_name$request_uri? permanent;
    }
    

    /etc/uwsgi/emperor.ini

    [uwsgi]
    emperor = /etc/uwsgi/vassals
    #master = true
    #plugins = python2
    uid = http
    gid = http
    

    /etc/uwsgi/vassals

    [uwsgi]
    chdir = /srv/http/test-dir/src
    wsgi-file = run.py
    callable = app
    processes = 4
    threads = 2
    offload-threads = 2
    stats =  127.0.0.1:9191
    max-requests = 5000
    enable-threads = true
    #master = true
    vacuum = true
    #socket = 127.0.0.1:3031
    socket = /run/uwsgi/test.sock
    chmod-socket = 664
    harakiri = 60
    logto = /var/log/uwsgi/test.log
    

    /var/log/uwsgi/test.log

    *** Starting uWSGI 2.0.9 (64bit) on [Wed Mar 25 17:17:53 2015] ***
    compiled with version: 4.9.2 20150204 (prerelease) on 28 February 2015 13:34:10
    os: Linux-3.19.2-1-ARCH #1 SMP PREEMPT Wed Mar 18 16:21:02 CET 2015
    nodename: MyServer
    machine: x86_64
    clock source: unix
    pcre jit disabled
    detected number of CPU cores: 2
    current working directory: /etc/uwsgi/vassals
    detected binary path: /usr/bin/uwsgi
    chdir() to /srv/http/test-dir/src
    your processes number limit is 13370
    your memory page size is 4096 bytes
     *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers *** 
    detected max file descriptor number: 1024
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    uwsgi socket 0 bound to UNIX address /run/uwsgi/test.sock fd 3
    your server socket listen backlog is limited to 100 connections
    your mercy for graceful operations on workers is 60 seconds
    mapped 415360 bytes (405 KB) for 8 cores
    *** Operational MODE: preforking+threaded ***
    *** no app loaded. going in full dynamic mode ***
    *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI master process (pid: 1349)
    spawned uWSGI worker 1 (pid: 1350, cores: 2)
    spawned uWSGI worker 2 (pid: 1351, cores: 2)
    spawned 2 offload threads for uWSGI worker 1
    spawned uWSGI worker 3 (pid: 1355, cores: 2)
    spawned uWSGI worker 4 (pid: 1356, cores: 2)
    *** Stats server enabled on 127.0.0.1:9191 fd: 16 ***
    spawned 2 offload threads for uWSGI worker 3
    spawned 2 offload threads for uWSGI worker 4
    spawned 2 offload threads for uWSGI worker 2
    -- unavailable modifier requested: 0 --
    announcing my loyalty to the Emperor...
    -- unavailable modifier requested: 0 --
    announcing my loyalty to the Emperor...
    

    As stated I am generating a 502 error when I connect to the servers internal address, this is coming out of the nginx error log:

    2015/03/25 17:21:07 [error] 1340#0: *3 upstream prematurely closed connection while reading response header from upstream, client: [me], server: 192.xxx.x.xxx, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/run/uwsgi/test.sock:", host: "192.xxx.x.xxx"
    

    http owns the socket so: ls -ld /run/uwsgi/test.sock

    srw-rw-r-- 1 http http 0 Mar 25 17:17 /run/uwsgi/test.sock
    

    I think I have everything on the uwsgi side installed: sudo pacman -Ss uwsgi

    community/mod_proxy_uwsgi 2.0.9-3
        Apache uWSGI proxy module
    community/uwsgi 2.0.9-3 [installed]
        A fast, self-healing and developer/sysadmin-friendly application container server coded in pure C
    community/uwsgi-plugin-cgi 2.0.9-3
        CGI plugin
    community/uwsgi-plugin-jvm 2.0.9-3
        Plugin for Jvm support
    community/uwsgi-plugin-lua51 2.0.9-3
        Plugin for Lua support
    community/uwsgi-plugin-mono 2.0.9-3
        Plugin for mono support
    community/uwsgi-plugin-php 2.0.9-3
        Plugin for PHP support
    community/uwsgi-plugin-psgi 2.0.9-3
        Perl psgi plugin
    community/uwsgi-plugin-pypy 2.0.9-3
        Plugin for PyPy support
    community/uwsgi-plugin-python 2.0.9-3
        Plugin for Python support
    community/uwsgi-plugin-python2 2.0.9-3 [installed]
        Plugin for Python2 support
    community/uwsgi-plugin-rack 2.0.9-3
        Ruby rack plugin
    community/uwsgi-plugin-webdav 2.0.9-3
        Plugin for webdav support
    

    I had plugins = python2 enabled [yes this is python2.7] in the main emperor.ini but it didn't do anything, either it is not needed or in the wrong spot. I have read a couple of other things here and at each packages site, not quite sure what I am missing though, but it is something. Thanks.

    3/26/2015 EDIT

    I removed my distributions version of uwsgi and installed it via pip, seems to be behaving better.

    I began just using command line configs: sudo uwsgi --http 192.xxx.x.xxx:80 --wsgi-file run.py -callable app --enable-threads --chdir /srv/http/test-dir/src --processes 4 --threads 2 --offload-threads 2 --vacuum --harakiri 60

    *** Starting uWSGI 2.0.10 (64bit) on [Thu Mar 26 21:16:33 2015] ***
    compiled with version: 4.9.2 20150304 (prerelease) on 26 March 2015 20:52:38
    os: Linux-3.19.2-1-ARCH #1 SMP PREEMPT Wed Mar 18 16:21:02 CET 2015
    nodename: MyServer
    machine: x86_64
    clock source: unix
    pcre jit disabled
    detected number of CPU cores: 2
    current working directory: /srv/http/test-dir/src
    detected binary path: /usr/bin/uwsgi
    uWSGI running as root, you can use --uid/--gid/--chroot options
    *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
    chdir() to /srv/http/test-dir/src
    *** WARNING: you are running uWSGI without its master process manager ***
    your processes number limit is 13370
    your memory page size is 4096 bytes
     *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
    detected max file descriptor number: 16384
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    uWSGI http bound on 192.168.1.104:80 fd 4
    spawned uWSGI http 1 (pid: 4530)
    uwsgi socket 0 bound to TCP address 127.0.0.1:34517 (port auto-assigned) fd 3
    Python version: 2.7.9 (default, Dec 11 2014, 04:42:00)  [GCC 4.9.2]
    Python main interpreter initialized at 0x120aac0
    python threads support enabled
    your server socket listen backlog is limited to 100 connections
    your mercy for graceful operations on workers is 60 seconds
    mapped 332288 bytes (324 KB) for 8 cores
    *** Operational MODE: preforking+threaded ***
    WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x120aac0 pid: 4529                                                                                                                                                              (default app)
    *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI worker 1 (pid: 4529, cores: 2)
    spawned uWSGI worker 2 (pid: 4533, cores: 2)
    spawned 2 offload threads for uWSGI worker 2
    spawned uWSGI worker 3 (pid: 4536, cores: 2)
    spawned uWSGI worker 4 (pid: 4537, cores: 2)
    spawned 2 offload threads for uWSGI worker 1
    spawned 2 offload threads for uWSGI worker 4
    spawned 2 offload threads for uWSGI worker 3
    

    I got this error:

    sqlalchemy.exc.ProgrammingError: (ProgrammingError) SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140554959951616 and this is thread id 140555111335808

    removing --enable-threads does nothing. A user would largely be loading and unloading objects onto the flask session object at the web page interaction that can cause this error, doesn't happen all the time, what can I do to get around this?

    • Fernando Cordeiro
      Fernando Cordeiro about 9 years
      Gotta test separately. I suggest you first try to access static files on nginx. Then try accessing server-ip:3031. Update us on what works and doesn't work, so we can help you. :D