简体   繁体   English

nginx中的uwsgi .ini参数等效项(flask应用程序上出现500个错误)

[英]uwsgi .ini param equivalents in nginx (500 errors on flask app)

I have been attempting to move my flask code into uwsgi with nginx. 我一直在尝试使用Nginx将Flask代码移到uwsgi中。 It's gone through 502 (wrong socket filename) to 404 (default app 0 - not loading runable code in nginx config), to now a 500, and I have no info on why. 它经历了从502(错误的套接字文件名)到404(默认应用0-不在nginx配置中加载可运行的代码)的过程,现在变成了500,并且我没有关于为什么的信息。

  • A) should uwsgi params be in nginx or uwsgi? A)uwsgi参数应该在nginx还是uwsgi中?
  • B) app name seems so picky all the time. B)应用名称似乎一直都很挑剔。 is it just me? 只有我吗?

Slowly but surely the modifications seem to be coming from problems in my uwsgi.ini or nginx.sites files, and I really just want someone to vet them and explain what's happening. 慢慢地但可以肯定的是,修改似乎是由于我的uwsgi.ini或nginx.sites文件中的问题引起的,我真的只是希望有人来审核它们并解释发生了什么。

uwsgi.ini: 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 config: 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;
    }
}

uwsgi.py: uwsgi.py:

#!/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)

which one is supposed to properly help me start the server under uwsgi's control? 哪一个应该可以正确地帮助我在uwsgi的控制下启动服务器?

I've created a git branch with only the more relevant code if needed (and there's always the master branch): https://github.com/Thetoxicarcade/britishQueue/tree/flaskBug 我已经创建了一个git分支,如果需要的话,它只包含更相关的代码(并且总有master分支): https : //github.com/Thetoxicarcade/britishQueue/tree/flaskBug

edit (using production's log file): 编辑(使用生产的日志文件):

*** 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)

Let's divide it to steps. 让我们将其分为几步。

Built-in HTTP server 内置HTTP服务器

First, your application seems to have some built-in http server, let's run it. 首先,您的应用程序似乎具有一些内置的http服务器,让我们运行它。 Just call python wsgi.py and on address 127.0.0.1:6002 will be your application (if you're doing it on remote server, replace 127.0.0.1 with his IP address). 只需调用python wsgi.py ,地址为127.0.0.1:6002就是您的应用程序(如果在远程服务器上进行操作,请用其IP地址替换127.0.0.1)。

This will help you narrow errors in application. 这将帮助您缩小应用程序中的错误。

If this works fine, stop that application server (probably ctrl+c or ctrl+d). 如果正常,请停止该应用程序服务器(可能是ctrl + c或ctrl + d)。

uWSGI server uWSGI服务器

Now, when you're sure that app is working, let's try to put it behind uWSGI. 现在,当您确定该应用程序正在运行时,让我们尝试将其放在uWSGI之后。 Here is configuration that works for me every time: 这是每次都适用的配置:

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

After running that uWSGI server, go to 127.0.0.1:8000 in your browser. 运行该uWSGI服务器后,在浏览器中转到127.0.0.1:8000 That should give your same results as running application built-in server. 与运行应用程序内置服务器的结果相同。 If not, check uWSGI logs (located in your project dir by default) for clue why it isn't working. 如果没有,请检查uWSGI日志(默认情况下位于您的项目目录中),以了解为什么它不起作用。

If uWSGI is working fine, uncomment in it's config lines with socket and comment http one (or remove). 如果uWSGI工作正常,请在socket的配置行中取消注释,并注释http one (或删除)。

nginx server Nginx服务器

Here is my config for nginx to communicate with uWSGI app: 这是我的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;
    }
}

And that's it, your app should be accessible on address intern.alpha.aristatek.com . 就是这样,您的应用应该可以在地址intern.alpha.aristatek.com上访问。 If it's not, check nginx errors and if nginx is running on www-data user and group and if not, change groupname for uwsgi socket in your uWSGI config file. 如果不是,请检查nginx错误,如果nginx在www-data用户和组上运行,否则,请在uWSGI配置文件中更改uwsgi套接字的组名。

Uwsgi needs to be directly told to use an async-like method. 需要直接告知Uwsgi使用类似异步的方法。 In my case, that's: 就我而言,那就是:

[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]

Which works fantastically and lets the threads that fail, fail on their own :) 哪个效果很好,让失败的线程自行失败:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM