[英]Python flask & fast-cgi on apache - 500 Internal Server Error (Premature end of script headers)
我想在apache產品服務器上設置我的flask應用程序。 我創建了以下.htaccess文件:
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
<Files ~ (\.fcgi)>
SetHandler fcgid-script
Options +SymLinksIfOwnerMatch +ExecCGI
</Files>
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /fcgi-bin/runFlaskApp.fcgi/$1 [QSA,L]
</IfModule>
哪個應重定向到以下fcgi文件(設置為chmod 755):
#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-
RELATIVE_WEB_URL_PATH = '/app_dict'
import os
# This points to the application on the local filesystem.
LOCAL_APPLICATION_PATH = os.path.expanduser('~') + '/html/app_dict'
import sys
sys.path.insert(0, LOCAL_APPLICATION_PATH)
from flup.server.fcgi import WSGIServer
from tmain import app
class ScriptNamePatch(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = RELATIVE_WEB_URL_PATH
return self.app(environ, start_response)
app = ScriptNamePatch(app)
if __name__ == '__main__':
WSGIServer(app).run()
作為回報,應啟動以下燒瓶應用程序:
#!/usr/bin/env python2.7
# -*- coding: utf8 -*-
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
但是,當我嘗試訪問該網站時,顯示內部服務器錯誤500。 並且以下行顯示在apache錯誤日志中:
[warn] [client 0.0.0.0] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[error] [client 0.0.0.0] Premature end of script headers: runFlaskApp.fcgi
如果我使用“ python2.7 runFlaskApp.fcgi”運行fcgi文件,它將返回以下內容:
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 12
Hello World!
我相信標頭錯誤是因為它不在WSGIServer的上下文中運行。
我已經嘗試通過添加錯誤格式的代碼行來在flask應用程序中引發異常。 但是apache錯誤日志中沒有異常顯示。 這使我相信,fcgi腳本甚至沒有加載flask應用程序。
有沒有辦法進一步調試它,或者有人知道該問題的解決方案嗎?
問題出在fcgi文件的版權或行尾。 將其從Windows上傳到服務器並設置chmod 755無效。 僅在服務器上創建文件然后運行chmod 755對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.