我正在尝试使用apache mod_wsgi来创建一个带有python flask应用程序的守护进程。我想验证
from flask import Flask
import flask_httpauth
app = Flask(__name__)
auth = flask_httpauth.HTTPBasicAuth()
@app.route('/')
def hello_world():
return 'Hello from flaskapp.py!'
@app.route('/private')
@auth.login_required
def check_credentials():
if auth.username()=='myself':
if password == 'secret':
return 'Valid'
else: return 'Wrong'
return None
if __name__ == '__main__':
app.run()
我在/etc/apache2/sites-enabled/000-default.conf中添加了一个WSGI守护程序部分,如下所示:
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias /flaskapp /var/www/html/flaskapp/flaskapp.wsgi
WSGIPassAuthorization On
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
flaskapp.wsgi文件:
import sys
sys.path.insert(0, '/var/www/html/flaskapp')
from flaskapp import app as application
当我尝试在REST客户端(chrome)中访问这些URL时,
因此,flashkapp.py模块(使用@ auth.login)作为普通的烧瓶应用程序工作,但在mod_wsgi守护进程中没有按预期工作。
该应用在AWS EC2 Ubuntu实例上运行。
知道出了什么问题吗?
更新:(谢谢@Graham Dumpleton)。按建议添加了日志记录:
class LoggingMiddleware:
def __init__(self, application):
self.__application = application
def __call__(self, environ, start_response):
errors = environ['wsgi.errors']
pprint.pprint(('REQUEST', environ), stream=errors)
def _start_response(status, headers, *args):
pprint.pprint(('RESPONSE', status, headers), stream=errors)
return start_response(status, headers, *args)
return self.__application(environ, _start_response)
application = LoggingMiddleware(app.app_context())
现在apache错误日志如下:
'REQUEST',
{'CONTENT_TYPE': 'application/json',
'CONTEXT_DOCUMENT_ROOT': '/var/www/html',
'CONTEXT_PREFIX': '',
'DOCUMENT_ROOT': '/var/www/html',
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_HOST': 'ec2-nn-nn-nnn-nnn.us-xxxx-n.compute.amazonaws.com',
'PATH_INFO': '/private',
'PATH_TRANSLATED': '/var/www/html/private',
'QUERY_STRING': '',
'REMOTE_ADDR': '122.167.143.128',
'REMOTE_PORT': '12810',
'REQUEST_METHOD': 'GET',
'REQUEST_SCHEME': 'http',
'REQUEST_URI': '/flaskapp/private',
'SCRIPT_FILENAME': '/var/www/html/flaskapp/flaskdbgapp.wsgi',
'SCRIPT_NAME': '/flaskapp',
'SERVER_ADDR': 'nnn.nn.nn.nnn',
'SERVER_ADMIN': 'webmaster@localhost',
'SERVER_NAME': 'ec2-nn-nn-nnn-nnn.us-xxxx-n.compute.amazonaws.com',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SIGNATURE': '<address>Apache/2.4.7 (Ubuntu) Server at ec2-nn-nn-nnn-nnn.us-xxxx-2.compute.amazonaws.com Port 80</address>\\n',
'SERVER_SOFTWARE': 'Apache/2.4.7 (Ubuntu)',
'mod_wsgi.application_group': 'ip-nnn-nn-nn-nnn.us-xxxx-2.compute.internal|/flaskapp',
'mod_wsgi.callable_object': 'application',
'mod_wsgi.enable_sendfile': '0',
'mod_wsgi.handler_script': '',
'mod_wsgi.input_chunked': '0',
'mod_wsgi.listener_host': '',
'mod_wsgi.listener_port': '80',
'mod_wsgi.process_group': '',
'mod_wsgi.queue_start': '1489237814187968',
'mod_wsgi.request_handler': 'wsgi-script',
'mod_wsgi.script_reloading': '1',
'mod_wsgi.version': (3, 4),
'wsgi.errors': <mod_wsgi.Log object at 0x7f0d96a77f30>,
'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x7f0d96a79990>,
'wsgi.input': <mod_wsgi.Input object at 0x7f0d96a77a30>,
'wsgi.multiprocess': True,
'wsgi.multithread': True,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)})
'RESPONSE',
'401 UNAUTHORIZED',
[('Content-Type', 'text/html; charset=utf-8'),
('Content-Length', '19'),
('WWW-Authenticate', 'Basic realm="Authentication Required"')])
仍无法找到解决方案: - (