NGINX,带有 CGI 插件的 uWSGI,Python 脚本 - 502 Bad Gateway

NGINX, uWSGI with CGI plugin, Python scripts - 502 Bad Gateway

我想使用带有 CGI 插件的 uWSGI 来处理从 NGINX 传递给 .py 文件的所有请求。

NGINX 服务器配置:

location ~ \.py$ {
    include         uwsgi_params;
    uwsgi_modifier1 9;
    uwsgi_pass      unix:/var/www/html/cgi-bin/wsgi.sock;
}

uWSGI 配置:

[uwsgi]
plugin-dir = /usr/lib/uwsgi/plugins
plugins = cgi
cgi = /var/www/html/cgi-bin
cgi-allowed-ext = .py
cgi-helper = .py=python
master = true
processes = 5
socket = wsgi.sock
chmod-socket = 664
vacuum = true
die-on-term = true

uWSGI 起始输出:

$ sudo -u www-data uwsgi --ini uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.18 (64bit) on [Fri Oct 18 15:27:33 2019] ***
compiled with version: 7.4.0 on 18 October 2019 12:14:16
os: Linux-4.15.0-65-generic #74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019
nodename: some-host
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /var/www/html/cgi-bin
detected binary path: /usr/bin/uwsgi
your processes number limit is 15501
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 wsgi.sock fd 3
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 437424 bytes (427 KB) for 5 cores
*** Operational MODE: preforking ***
initialized CGI path: /var/www/html/cgi-bin
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 6868)
spawned uWSGI worker 1 (pid: 6869, cores: 1)
spawned uWSGI worker 2 (pid: 6870, cores: 1)
spawned uWSGI worker 3 (pid: 6871, cores: 1)
spawned uWSGI worker 4 (pid: 6872, cores: 1)
spawned uWSGI worker 5 (pid: 6873, cores: 1)

/var/www/html/cgi-bin/ 中有 2 个文件:

如果我用任何其他文件名打开一个 URL,我会从 uWSGI 得到 404 错误——这很好。但是如果我用任何一个实际的脚本名称打开一个 URL,那么我会从 uWSGI 得到 500(因此从 NGINX 得到 502 Bad Gateway)。

URLs:

  1. http://localhost/another.py
  2. http://localhost/info.py
  3. http://localhost/some.py

输出:

[pid: 9606|app: -1|req: -1/1] 127.0.0.1 () {42 vars in 624 bytes} [Fri Oct 18 15:40:50 2019] GET /another.py => generated 9 bytes in 0 msecs (HTTP/1.1 404) 2 headers in 71 bytes (0 switches on core 0)
[pid: 9604|app: -1|req: -1/2] 127.0.0.1 () {42 vars in 618 bytes} [Fri Oct 18 15:41:01 2019] GET /info.py => generated 0 bytes in 15 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
[pid: 9606|app: -1|req: -1/3] 127.0.0.1 () {42 vars in 618 bytes} [Fri Oct 18 15:41:05 2019] GET /some.py => generated 0 bytes in 15 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

很明显它找到了文件(info.pysome.py),但是uWSGI的500错误的原因是什么?我的配置有问题吗?

这是 Python 脚本之一:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [ "Ololo".encode("utf-8") ]

使用 instructions from documentaion:

安装带有 CGI 插件的 uWSGI
curl http://uwsgi.it/install | bash -s cgi /tmp/uwsgi
sudo mv /tmp/uwsgi /usr/bin

我写了一篇 blog post 来详细说明此事。

你得到 500 分是因为 uWSGI CGI 插件不使用 WSGI,它使用 CGI(我花了几个小时才弄清楚这个区别)。您正在使用的 python 脚本实现了 WSG 接口(使用“应用程序”方法)但实际上并不是 CGI-compatible.

为了使其兼容,只需输出 HTTP 响应 headers,换行,然后将 HTTP body 输出到标准输出 (https://en.wikipedia.org/wiki/Common_Gateway_Interface#Example)。

因此对于您的示例,这将变为:

print("Content-Type: text/html")
print("Status: 200 OK")
print()

print("Ololo")