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

标签 python nginx cgi uwsgi

我想使用带有 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个文件:

  • info.py
  • some.py

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

网址:

  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),但是 500 错误的原因是什么? uWSGI?我的配置有问题吗?

这是 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 接口(interface)(使用“应用程序”方法),但实际上并不兼容 CGI。

为了使其兼容,只需将 HTTP 响应 header 、一个新行和 HTTP 正文输出到标准输出 ( https://en.wikipedia.org/wiki/Common_Gateway_Interface#Example )。

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

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

print("Ololo")

关于python - NGINX、带有 CGI 插件的 uWSGI、Python 脚本 - 502 Bad Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452419/

相关文章:

python - 无法更改框架中函数的值

ubuntu - 卸载nginx?

docker - 使用 nginx 的动态 ssl 证书

docker - 我应该为我的Web应用程序的一部分创建多个Dockerfile吗?

perl - 如何为基本的 http 身份验证设置用户名和密码?

python - 根据字典键将字典值分配给 DataFrame 列

python - 抽象语法树中 Python f 字符串的行号

Python 2.7 - 将 base 64 转换为二进制字符串

jQuery AJAX 到 Perl JSON 模块数据解码

JavaScript 未与 Perl CGI 一起运行