Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行nginx+;python flask+;python守护进程:上游发送了不受支持的FastCGI协议版本91_Python_Nginx_Daemon_Fastcgi_Flask - Fatal编程技术网

运行nginx+;python flask+;python守护进程:上游发送了不受支持的FastCGI协议版本91

运行nginx+;python flask+;python守护进程:上游发送了不受支持的FastCGI协议版本91,python,nginx,daemon,fastcgi,flask,Python,Nginx,Daemon,Fastcgi,Flask,我使用PythonFlask通过带有nginx的FCGI运行PythonforWeb。我的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__':

我使用PythonFlask通过带有nginx的FCGI运行PythonforWeb。我的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()
如果我在没有daemon参数的情况下运行WSGIServer,它可以正常工作

但是如果我使用守护进程参数运行它,我会在nginx日志中得到这个错误,而对服务器的任何请求都以“502坏网关”结尾:


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

事实证明,DaemonContext关闭了任何打开的文件描述符,因此基本上应该有一个函数,它实例化WSGIServer,以及appа以及可以在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()

如果你玩一点cwd和uid,它会改变什么吗?例如,在那里传递其他值,然后默认为一个?在最坏的情况下,使用“strace-fo/tmp/log XXXXX yyyy”运行它,其中是您的起始命令行序列
#!/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()