2012-05-09 29 views
3

我正在使用Python Flask通過FCGI和nginx運行Python for web。我的fcgi後端設置是這樣的:運行nginx + python flask + python-daemon:upstream發送不支持的FastCGI協議版本91

#!/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網關錯誤」:

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" 

任何想法,爲什麼會出現這種情況和如何預防它?

+0

它是否在改變什麼,如果你打了一下與CWD和uid?例如傳遞其他值然後默認一個?在最糟的情況下,用'strace -fo/tmp/log XXXXX yyyy'運行它,你的起始命令行序列在哪裏 – bluszcz

回答

0

事實證明,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()