gpt4 book ai didi

python - 当运行 nginx + python flask + python-daemon : upstream sent unsupported FastCGI protocol version 91

转载 作者:行者123 更新时间:2023-11-28 19:26:29 25 4
gpt4 key购买 nike

我正在使用 Python Flask 通过 FCGI 和 nginx 运行 Python for web。我的 fcgi 后端设置如下:

#!/usr/bin/env python
import argparse, daemon, os
from flup.server.fcgi import WSGIServer
from fwd_msg import app

SOCKET_LOCATION = '/tmp/fingerprinter-fcgi.sock'

if __name__ == '__main__':
# arg parse (and daemonize)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--daemon', action='store_true', default=False, help='Run as daemon')
arg_parser.add_argument('--cwd', action='store', default='/',
help='Full path of the working directory to which the process should change on daemon start.')
arg_parser.add_argument('--uid', action='store', type=int, default=os.getuid(),
help='The user ID ("UID") value and group ID ("GID") value to switch the process to on daemon start.')
args = vars(arg_parser.parse_args())

if args['daemon']:
context = daemon.DaemonContext(working_directory=args['cwd'], uid=args['uid'])
with context:
WSGIServer(app, bindAddress=SOCKET_LOCATION).run()
else:
WSGIServer(app, bindAddress=SOCKET_LOCATION).run()

如果我在没有守护进程参数的情况下运行 WSGIServer,它工作正常。

但是如果我使用守护程序参数运行它,我会在 nginx 日志中收到此错误,而对服务器的任何请求都以“502 BAD GATEWAY”结尾:

2012/05/09 12:16:00 [error] 30895#0: *30 upstream sent unsupported FastCGI protocol version: 91 while reading response header from upstream, client: XXX.XXX.XXX.XXX, server: localhost, request: "POST / HTTP/1.1", upstream: "fastcgi://unix:/tmp/fingerprinter-fcgi.sock:", host: "XXX.XXX.XXX.XXX"

知道为什么会发生这种情况以及如何预防吗?

最佳答案

事实证明,DaemonContext 会关闭所有打开的文件描述符,所以基本上应该有一个函数,它实例化 WSGIServer,连同应用程序以及可以在 DaemonContext 中打开文件描述符的所有内容。

还要确保工作目录归用户所有,或者至少具有允许具有给定 UID 的用户在那里写入的权限(不推荐)。

例子:

#!/usr/bin/env python
import argparse, daemon, os
from flup.server.fcgi import WSGIServer
from fwd_msg import app

SOCKET_LOCATION = '/tmp/fingerprinter-fcgi.sock'

def main():
app = flask.Flask(__name__)
@app.route('/', methods=['GET'])
def index():
pass # your actions here

if __name__ == '__main__':
# arg parse (and daemonize)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--daemon', action='store_true', default=False, help='Run as daemon')
arg_parser.add_argument('--cwd', action='store', default='/',
help='Full path of the working directory to which the process should change on daemon start.')
arg_parser.add_argument('--uid', action='store', type=int, default=os.getuid(),
help='The user ID ("UID") value and group ID ("GID") value to switch the process to on daemon start.')
args = vars(arg_parser.parse_args())

if args['daemon']:
context = daemon.DaemonContext(working_directory=args['cwd'], uid=args['uid'])
with context:
main()
else:
main()

关于python - 当运行 nginx + python flask + python-daemon : upstream sent unsupported FastCGI protocol version 91,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10516007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com