502 错误网关 nginx.uwsgi、flask

502 错误网关 nginx.uwsgi、flask

好的,我用 python 开发了一个 Web 应用程序,它可以与内部服务器一起工作:

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

完全没有问题。我现在正在尝试启动一个在 arch linux 上运行 uwsgi 的 nginx 服务器,下面是配置文件:

我的应用程序的 config.py:

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...

如上所述,当我连接到服务器内部地址时生成 502 错误,这是来自 nginx 错误日志:

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 拥有套接字,因此: ls -ld /run/uwsgi/test.sock

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

我认为我已经安装了 uwsgi 端的所有内容: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

plugins = python2在主 Emperor.ini 中启用了 [是的,这是 python2.7],但它没有做任何事情,要么是不需要,要么是在错误的位置。我在这里和每个软件包网站上读了一些其他的东西,虽然不太确定我错过了什么,但确实有一些东西。谢谢。

2015/3/26 编辑

我删除了我的发行版的 uwsgi 并通过 pip 安装了它,似乎效果更好。

我开始只是使用命令行配置: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

我收到此错误:

sqlalchemy.exc.ProgrammingError:(ProgrammingError)在线程中创建的 SQLite 对象只能在同一个线程中使用。该对象是在线程 ID 140554959951616 中创建的,这是线程 ID 140555111335808

删除 --enable-threads 不会产生任何效果。用户在网页交互时会大量加载和卸载对象到 flask 会话对象上,这可能会导致此错误,但这种情况并不常见,我该如何解决这个问题?

答案1

我认为有两件事有助于解决这个PROPAGATE_EXCEPTIONS = True问题配置.py我从我的vassal ini文件中删除了线程,结尾的uwsgi文件如下所示:

/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

不确定,PROPAGATE_EXCEPTIONS = True但删除 test.ini 中的线程选项并确保 Emperor.ini 中有一个主选项似乎已经解决了 sql 被抛到不同线程的问题,或者至少它对此抱怨并使网站崩溃,要么。

相关内容