nginx中的uwsgi .ini param等价物(烧瓶app上有500个错误)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx中的uwsgi .ini param等价物(烧瓶app上有500个错误)相关的知识,希望对你有一定的参考价值。

我一直试图用nginx将我的烧瓶代码移动到uwsgi中。它经历了502(错误的套接字文件名)到404(默认应用0 - 没有加载nginx配置中的可运行代码),到现在为500,我没有关于原因的信息。

  • A)uwsgi params应该是nginx还是uwsgi?
  • B)应用程序名称似乎一直都很挑剔。只有我吗?

慢慢但肯定地,修改似乎来自我的uwsgi.ini或nginx.sites文件中的问题,我真的只是希望有人审查它们并解释发生了什么。

uwsgi.ini:

# can't these take over from nginx?
[uwsgi]
base = /home/aristatek/britishQueue
home = /home/aristatek/britishQueue
venv = /home/aristatek/britishQueue/clearable
socket = /home/aristatek/britishQueue/uwsgi.sock
chmod-socket = 666
wsgi-file = uwsgi.py
callable = application
daemonize=/home/aristatek/britishQueue/uwsgi.log
master = true
processes = 4
threads = 2

nginx配置:

server {
    server_name intern.alpha.aristatek.com default_server;
    listen 80;
    location / {
        root /home/aristatek/britishQueue/srv/;
    }
    location /api/ { try_files $uri @britishQueue; }
    location @britishQueue {
        include uwsgi_params;
        uwsgi_param UWSGI_BASE /home/aristatek/britishQueue;
        uwsgi_param UWSGI_HOME /home/aristatek/britishQueue;
        uwsgi_param UWSGI_VENV /home/aristatek/britishQueue/clearable;
        uwsgi_param UWSGI_MODULE uwsgi;
        uwsgi_param UWSGI_CALLABLE application;
        uwsgi_pass unix:/home/aristatek/britishQueue/uwsgi.sock;
    }
}

u完善GI.朋友:

#!/usr/bin/env python
from britishQueue import webserve as application # why so picky all the time..

if __name__ == "__main__":
    application.run(host='0.0.0.0', port=6002, debug=True)

哪一个应该能够正确地帮助我在uwsgi的控制下启动服务器?

我已经创建了一个git分支,如果需要,只有更相关的代码(并且总是有主分支):https://github.com/Thetoxicarcade/britishQueue/tree/flaskBug

编辑(使用生产的日志文件):

*** Starting uWSGI 2.0.12 (64bit) on [Tue Jan 19 14:40:02 2016] ***
compiled with version: 4.8.4 on 13 January 2016 14:22:40
os: Linux-3.16.0-57-generic #77~14.04.1-Ubuntu SMP Thu Dec 17 23:20:00 UTC 2015
nodename: intern.alpha.aristatek.com
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /home/aristatek/britishQueue/flask
detected binary path: /usr/local/bin/uwsgi
your processes number limit is 31835
your memory page size is 4096 bytes
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 /home/aristatek/britishQueue/flask/uwsgi.sock fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
Set PythonHome to /home/aristatek/britishQueue/clearable
Python main interpreter initialized at 0x1ab2c80
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 415360 bytes (405 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1ab2c80 pid: 1148 (default app)
spawned uWSGI master process (pid: 1148)
spawned uWSGI worker 1 (pid: 1280, cores: 2)
spawned uWSGI worker 2 (pid: 1281, cores: 2)
spawned uWSGI worker 3 (pid: 1283, cores: 2)
spawned uWSGI worker 4 (pid: 1286, cores: 2)
[pid: 1286|app: 0|req: 1/1] 192.168.2.60 () {54 vars in 1147 bytes} [Tue Jan 19 14:40:14 2016] GET /api/v1.0/status/ => generated 291 bytes in 30 msecs (HTTP/1.1 500) 2 headers in 84 bytes (1 switches on core 0)
[pid: 1286|app: 0|req: 2/2] 192.168.2.60 () {54 vars in 1147 bytes} [Tue Jan 19 14:40:15 2016] GET /api/v1.0/status/ => generated 291 bytes in 1 msecs (HTTP/1.1 500) 2 headers in 84 bytes (1 switches on core 1)
[pid: 1286|app: 0|req: 3/3] 192.168.2.60 () {54 vars in 1147 bytes} [Tue Jan 19 14:40:15 2016] GET /api/v1.0/status/ => generated 291 bytes in 1 msecs (HTTP/1.1 500) 2 headers in 84 bytes (1 switches on core 0)
答案

我们把它分成几步。

内置HTTP服务器

首先,你的应用程序似乎有一些内置的http服务器,让我们运行它。只需调用python wsgi.py,地址127.0.0.1:6002将成为您的应用程序(如果您在远程服务器上执行此操作,请将127.0.0.1替换为其IP地址)。

这将帮助您缩小应用程序中的错误。

如果这样可以正常工作,请停止该应用程序服务器(可能是ctrl + c或ctrl + d)。

uWSGI服务器

现在,当您确定应用程序正在运行时,让我们尝试将其置于uWSGI之后。这是每次都适合我的配置:

chdir                   = /home/aristatek/britishQueue # put here directory, where you're storing your app code
uid                     = aristatek # username and groupname on which your app will be running (if you're running uWSGI as root, you really should do this, for security reasons)
gid                     = aristatek
auto-procname           = 1 # this 2 options will set user-readable name for your uWSGI processes, in this example each processname will be prefixed by your username and app name
procname-prefix-spaced  = [aristatek_britishQueue]

#chmod-socket            = 660 # this 2 options will ensure that your socket will be readable by user that is running app and uWSGI, no one else.
#chown-socket            = aristatek:www-data
#socket                  = /home/aristatek/britishQueue/uwsgi.sock
http                    = :8000
pidfile2                = /home/aristatek/britishQueue/uwsgi.pid

virtualenv              = /home/aristatek/britishQueue/clearable
workers                 = 4
threads                 = 2
master                  = true
module                  = wsgi # python path to your wsgi file, relative to main project dir
enable-threads          = 1

logger                  = file:/home/aristatek/britishQueue/uWSGI.log

运行该uWSGI服务器后,请在浏览器中访问127.0.0.1:8000。这应该与运行应用程序内置服务器的结果相同。如果没有,请检查uWSGI日志(默认情况下位于项目目录中),以找出它无法正常工作的原因。

如果uWSGI正常工作,请在其配置行中使用socket和注释http one(或删除)取消注释。

nginx服务器

这是我的nginx配置与uWSGI应用程序通信:

server {

    listen 80;
    server_name intern.alpha.aristatek.com default_server;

    client_max_body_size 4G;

    root /home/aristatek/britishQueue/srv/;

    location @britishQueue {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        include /etc/nginx/uwsgi_params;

        uwsgi_pass unix:/home/aristatek/britishQueue/uwsgi.sock;
        break;
    }

    location /api/ {
        try_files $uri @default;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        try_files /500.html /error.html /error500.html;
    }
}

就是这样,您的应用程序应该可以通过地址intern.alpha.aristatek.com访问。如果不是,请检查nginx错误,如果nginx在www-data用户和组上运行,如果没有,请在uWSGI配置文件中更改uwsgi套接字的groupname。

另一答案

需要直接告诉Uwsgi使用类似异步的方法。就我而言,那是:

[uwsgi]
plugins                 = python
virtualenv              = /home/aristatek/britishQueue/clearable
chdir                   = /home/aristatek/britishQueue/flask
module                  = britishQueue
callable                = webserve

uid                     = aristatek
gid                     = aristatek

#http                    = :6002
socket                  = /home/aristatek/britishQueue/flask/uwsgi.sock
chown-socket            = www-data:www-data
chmod-socket            = 666
pidfile2                = /home/aristatek/britishQueue/flask/uwsgi.pid
stats                   = /home/aristatek/britishQueue/flask/uwsgi.stats

#master                  = true
ugreen                  = true
async                   = 4
#workers                 = 4
#enable-threads          = 1
#threads                 = 4
#auto-procname           = 1
#procname-prefix-spaced  = [aristatek_britishQueue]

哪个非常有效,让失败的线程自己失败:)

以上是关于nginx中的uwsgi .ini param等价物(烧瓶app上有500个错误)的主要内容,如果未能解决你的问题,请参考以下文章

怎么用uwsgi 运行uwsgi.ini

Nginx与uWSGI交互

uwsgi+django+nginx

centos6.8 搭建nginx+uwsgi+Flask

Ubuntu18部署uwsgi+flask应用

nginx+uwsgi02---django部署