0

我有一个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()

前端 Web 服务器是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 脚本和我的 Python 脚本都在 machine 上运行时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,然后又发出了 a 404,就好像路径不知道一样。另一方面,当直接启动时(没有flup和使用相同的路由),它返回hello.

这种行为差异从何而来?

4

1 回答 1

0

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

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

于 2016-12-22T18:35:00.350 回答