nginx 中的 uwsgi .ini 参数等效项(flask 应用程序出现 500 个错误)
uwsgi .ini param equivalents in nginx (500 errors on flask app)
我一直在尝试使用 nginx 将我的烧瓶代码移动到 uwsgi 中。它经历了 502(错误的套接字文件名)到 404(默认应用程序 0 - 未在 nginx 配置中加载可运行代码),到现在的 500,我不知道为什么。
- A) uwsgi 参数应该在 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;
}
}
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)
哪个应该可以正确地帮助我在 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)
让我们把它分成几步。
Built-in HTTP 服务器
首先,您的应用程序似乎有一些 built-in 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
。这应该会给出与 运行ning 应用程序 built-in 服务器相同的结果。如果没有,请检查 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
用户和组上 运行ning,如果不是,请在您的 uWSGI 配置文件中更改 uwsgi 套接字的组名。
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 中。它经历了 502(错误的套接字文件名)到 404(默认应用程序 0 - 未在 nginx 配置中加载可运行代码),到现在的 500,我不知道为什么。
- A) uwsgi 参数应该在 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;
}
}
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)
哪个应该可以正确地帮助我在 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)
让我们把它分成几步。
Built-in HTTP 服务器
首先,您的应用程序似乎有一些 built-in 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
。这应该会给出与 运行ning 应用程序 built-in 服务器相同的结果。如果没有,请检查 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
用户和组上 运行ning,如果不是,请在您的 uWSGI 配置文件中更改 uwsgi 套接字的组名。
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]
效果非常好,让失败的线程自行失败 :)