【问题标题】:Why does using FastCGI with lighttpd trigger a 404 when the routing is not changed?为什么在路由未更改的情况下,使用带有 lighttpd 的 FastCGI 会触发 404?
【发布时间】:2017-05-07 19:30:57
【问题描述】:

我有一个基于 bottle 的 Web 应用程序,我想将它迁移到 FastCGI:

# coding=utf-8
import bottle

class Webserver:
    def __init__(self):
        bottle.route('/api', ['GET', 'OPTIONS'], self.data)
        bottle.run(host='127.0.0.1', port=8081, debug=True, quiet=False)

    def data(self):
        return "hello"

    @bottle.error(404)
    def error404(error):
        print("404 Not Found from {0} @ {1}".format(bottle.request.remote_addr, bottle.request.url))

Webserver()

它按预期工作。

将其移至 FastCGI,我修改了 run() 调用:

# coding=utf-8
import bottle

class Webserver:
    def __init__(self):
        bottle.route('/api', ['GET', 'OPTIONS'], self.data)
        bottle.run(server='flup', host='127.0.0.1', port=8081, debug=True, quiet=False)

    def data(self):
        return "hello"

    @bottle.error(404)
    def error404(error):
        print("404 Not Found from {0} @ {1}".format(bottle.request.remote_addr, bottle.request.url))

Webserver()

前端网络服务器为lighttpd,其完整配置为:

root@domotique /e/lighttpd# cat lighttpd.conf
server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
#       "mod_rewrite",
        "mod_fastcgi",
        "mod_accesslog",
)


server.document-root        = "/var/www/html"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80
accesslog.filename          = "/var/log/lighttpd/access.log"

index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

fastcgi.server = (
    "/api" =>
        (
                (
                    "host" => "127.0.0.1",
                    "port" => 8081,
                    "check-local" => "disable"
                )
        )
)

当lighttpd 和我的Python 脚本都在机器10.200.0.7 上运行时,我在发出http 调用时得到以下信息:

root@srv ~# http 10.200.0.7/api
HTTP/1.1 404 Not Found
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Thu, 22 Dec 2016 08:02:20 GMT
Server: lighttpd/1.4.35

从lighttpd的角度来看:

root@10.200.0.7# cat /var/log/lighttpd/access.log
10.200.0.1 10.200.0.7 - [22/Dec/2016:08:02:20 +0000] "GET /api HTTP/1.1" 404 0 "-" "HTTPie/0.9.2"

从python脚本一:

root@10.200.0.7# python3 wtest.py
Bottle v0.12.7 server starting up (using FlupFCGIServer())...
Listening on http://127.0.0.1:8081/
Hit Ctrl-C to quit.

404 Not Found from 10.200.0.1 @ http://10.200.0.7/api/

所以调用通过lighttpd,到达wtest.py,后者又发出404,就好像路径不知道一样。另一方面,直接启动时(不带flup,路由相同),返回hello。

这种行为差异从何而来?

【问题讨论】:

    标签: python fastcgi bottle lighttpd flup


    【解决方案1】:

    较新版本的 lighttpd 支持 uwsgi 协议,因此这可能是比 FastCGI 更好的解决方案来运行您的瓶子应用程序:https://redmine.lighttpd.net/projects/lighttpd/wiki/HowToPythonWSGI

    在您的情况下,您是否配置了瓶使用的 Flup 以期待 FastCGI 协议? Flup 可以使用多种不同的协议,您必须将其配置为使用 FastCGI。前几个点击可能会有所帮助:https://www.google.com/#q=configuring+bottle+fastcgi+lighttpd

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2011-01-06
      相关资源
      最近更新 更多