我正在使用Python Flask通过nginx的FCGI运行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"你知道为什么会发生这种情况吗?如何防止呢?
发布于 2012-05-11 22:59:48
结果是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()https://stackoverflow.com/questions/10516007
复制相似问题