我使用的是不直接支持 wsgi 应用程序的共享 cpanel 托管计划。所以我必须使用 wsgiref CGIHandler 解决方法,如下所述:http://flask.pocoo.org/docs/0.12/deploying/cgi/ .
一切正常并产生预期的结果,但 url 中总是有这些额外的东西:“/cgi-bin/index.cgi/”python 应用程序似乎自动添加(以匹配它在调用时检测到的内容由 cgi 处理程序)。
例如,我希望它是 myhost.com/login/而不是 myhost.com/cgi-bin/index.cgi/login/,或者是 myhost.com/而不是 myhost.com/cgi-bin/index.cgi/.
所有这些较短版本的链接都运行良好,因为引擎重写规则已到位。我已经检查过了。这只是找到一种方法告诉 Flask 应用程序摆脱“/cgi-bin/index.cgi/”的问题。
我的一些代码:
cat www/.htaccess
# Redirect everything to CGI WSGI handler, but Don't interfere with static files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/index.cgi/$1 [L]
.
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
CGIHandler().run(app)
.
cat www/flasky/manage.py
#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role, Permission
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role,
Permission=Permission)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
.
有什么想法吗?
谢谢!
请您参考如下方法:
我已经找到了一个 hack for it.. 但它只是一个 hack :-( 如果我在 wsgiref CGIHandler 脚本中覆盖 SCRIPT_NAME 环境变量的值,那么它就可以完美地工作。
这是更新后的代码:
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
os.environ['SCRIPT_NAME'] = ''
CGIHandler().run(app)
.
基本上,无论 os.environ['SCRIPT_NAME'] 值是什么,每次请求页面时它都会作为 flask 应用程序 url 的前缀。
不过,我仍在寻找更优雅的“pythonic”解决方案..
