【问题标题】:How can I run a FastCGI script in root url (/ - without path)?如何在根 url (/ - 没有路径) 中运行 FastCGI 脚本?
【发布时间】:2016-07-24 03:53:57
【问题描述】:

我在 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/$1 [R=302,L]

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

【问题讨论】:

    标签: apache .htaccess fastcgi


    【解决方案1】:

    仅供参考:您可以使用

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

    对于 fastCGI 设置,看看这个链接是否有帮助。 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/$1 [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()
    

    【讨论】:

    • 虽然这在理论上可以回答问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    • 经过一些修改,这就是要走的路。但是,该应用程序仍将/index.fcgi 附加到每个url_for() 调用(我是否提到我正在使用Flask?)。但是我找到了Flask官方网站中描述的ScriptNameStripperflask.pocoo.org/docs/0.11/deploying/fastcgi/#configuring-apache
    【解决方案2】:

    我使用的 Apache 配置(用 cmets 解释每个部分的作用)是:

    # 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>
    

    【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2010-09-27
    • 2010-11-12
    • 2011-01-16
    相关资源
    最近更新 更多