【问题标题】:nginx FastCGI-strip off the location prefix?nginx FastCGI-strip掉位置前缀?
【发布时间】:2015-08-18 11:57:42
【问题描述】:

我正在使用 web.py、spawn_fcgi 和 nginx 在 Python 中编写一个 Web 应用程序。

假设我在 nginx 中有这个配置块:

location / {
    include fastcgi.conf;
    fastcgi_pass 127.0.0.1:9001;
}

如果我随后访问,比如说,http://example.com/api/test,那么 FastCGI 应用程序接收到 /api/test 作为其请求位置。 web.py 框架将在确定要执行的类时使用此位置。例如:

urls = ( "/api/.*", myClass )

如果我需要将此脚本放在网站上的另一个位置,就会出现问题。例如:

location /app {
    include fastcgi.conf;
    fastcgi_pass 127.0.0.1:9001;
}

现在,当我访问http://example.com/app/api/test 时,FastCGI 应用程序将/app/api/test 作为其位置。

它当然可以位于任何地方:例如http://example.com/sloppy_admin/My%20Web%20Pages/app/api/test。 :-)

我希望应用程序可重定位,因为将其安装在其他服务器上可能需要这样做(例如,它必须与其他服务器共享服务器)。坚持每台服务器都将其放置在同一个“虚拟子目录”中似乎也有点顽固。

现在,我的解决方法是这样做:

URL_PREFIX = "/app" # the same as the nginx location parameter
urls = ( URL_PREFIX+"/api/.*",myClass )

这里的问题是 1)这意味着脚本仍然需要在每个站点上进行编辑(不一定很糟糕,但至少不方便)和 2)URL_PREFIX 变量必须可以全局访问整个集合脚本 - 因为例如任何类或函数可能需要访问该位置,但它不需要包含前缀。

我正在使用自定义 python 包(例如,包含 init.py 脚本的目录)来简化组成应用程序的不同脚本的管理,但问题在于传递 URL_PREFIX 参数。示例:

app.py:

from myapp import appclass
import web

URL_PREFIX = "/app" # the same as the nginx location parameter
urls = ( URL_PREFIX+"/api/.*",appclass.myClass )
app = web.application(urls,globals())
if __name__ == "__main__":
    web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
    app.run()

myapp/appclass.py:

class myClass:
    def GET(self):
        global URL_PREFIX # this does not work!
        return URL_PREFIX

是否有 nginx 参数导致发送到 FastCGI 的路径与位置相关,或者在 web.py 中有更优雅的处理方式?

【问题讨论】:

    标签: nginx fastcgi web.py


    【解决方案1】:

    fastcgi.conf 文件应该包含您需要的所有配置选项。您可能还想查看fastcgi_split_path_info 指令。

    Nginx FastCGI documentation.

    【讨论】:

    • 我有这个问题:@fdmillion 介意告诉我你到底设置了什么 fastcgi_split_path_info?
    猜你喜欢
    • 2014-01-30
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2020-02-10
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多