我如何 运行 root url 中的 FastCGI 脚本(/ - 没有路径)?

How can I run a FastCGI script in root url (/ - without path)?

我在 Python/Flask 中创建了一个简单的应用程序,它有一个主页 url (www.site.com/)。我已经获得了一个 HostGator 共享帐户 来托管它,所以我只能访问 .htaccess,但没有其他 Apache 配置文件。

我为 运行 应用程序设置了一个 FastCGI 脚本(在 these instructions 之后),但它们要求 URL 具有 /index.fcgi 或任何其他路径。 我可以让根路径 (/) 直接由 FastCGI 脚本提供服务吗? 此外,像 /static//favicon.ico 这样的文件夹应该由 Apache 提供服务。

我现在 .htaccess 是:

AddHandler fcgid-script .fcgi
DirectoryIndex index.fcgi

RewriteEngine On
RewriteBase /
RewriteRule ^index.fcgi$ - [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.fcgi/ [R=302,L]

我不确定,但我认为 Apache 版本是 2.2。

仅供参考:您可以使用

检查 apache 版本
apachectl -V  # or /<pathwhereapachelives>/apachectl -V

对于 fastCGI 设置,看看这个 link 是否有帮助。 http://redconservatory.com/blog/getting-django-up-and-running-with-hostgator-part-2-enable-fastcgi/

基本上,如果您想运行 /中的脚本,您可以使用mod_rewrite直接提供静态文件(如下例中的/media) ,以及 WSGI 脚本服务的所有其他路径:

AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/ [QSA,L]

请注意,当您使用 Flask 时,您必须覆盖 SCRIPT_NAME 请求变量(如 here 所述),否则 url_for() 的结果将是一个字符串从 /index.fcgi/... 开始:

#!/usr/bin/python
#: optional path to your local python site-packages folder
import sys
sys.path.insert(0, '<your_local_path>/lib/python2.6/site-packages')

from flup.server.fcgi import WSGIServer
from yourapplication import app

class ScriptNameStripper(object):
   def __init__(self, app):
       self.app = app

   def __call__(self, environ, start_response):
       environ['SCRIPT_NAME'] = ''
       return self.app(environ, start_response)

app = ScriptNameStripper(app)

if __name__ == '__main__':
    WSGIServer(app).run()

我为此使用的 Apache 配置(注释解释了每个部分的作用)是:

# Basic starting point
DocumentRoot /path/to/document/root

# Handle static files (anything in /static/ but also robots.txt)
Alias /static /path/to/document/root/static
Alias /robots.txt /path/to/document/root/robots.txt

<Location /robots.txt>
    SetHandler default-handler
</Location>

<Location /static>
    SetHandler default-handler
</Location>

# Handle the FastCGI program on the root
Alias / /path/to/my_fastcgi_program.fgi/

# Handle FastCGI
<Location />
    Options ExecCGI

    AddHandler fcgid-script .pl
    AddHandler fcgid-script .fcgi

    Require all granted
</Location>