Search code examples
python-3.xflasklighttpd

flask hosted via lighttpd fastcgi returns 404's for mutlilevel paths


I have my flask application up and running in lighttpd using fastcgi, and it works well except all of my multi-level (e.g., /foo/page2) paths result in 404 errors, but the single-level paths work fine (e.g., /page1).

127.0.0.1 localhost:5080 - [06/Sep/2017:16:38:45 +0000] "GET /page1 HTTP/1.1" 200

127.0.0.1 localhost:5080 - [06/Sep/2017:16:39:07 +0000] "GET /foo/page2 HTTP/1.1" 404

I get flask's 404 error handler, and not lighttpd's.

When running the application via flask run the multilevel paths work fine.

127.0.0.1 - - [06/Sep/2017 11:44:56] "GET /page1 HTTP/1.1" 200

127.0.0.1 - - [06/Sep/2017 11:44:56] "GET /foo/page2 HTTP/1.1" 200

My lighttpd.conf looks like:

server.document-root = "/var/www/"

server.port = 5080
server.username = "foobar"
server.groupname = "foobar"

server.modules += (
    "mod_fastcgi",
    "mod_rewrite",
    "mod_alias",
    "mod_accesslog"
)

$HTTP["url"] !~ "^/static" {
    fastcgi.server = ("/" =>
        ((
            "socket" => "/tmp/foobar-fcgi.sock",
            "bin-path" => "/home/foobar/app.fcgi",
            "check-local" => "disable",
            "max-procs" => 1
        ))
    )
}

# give us debugging output
fastcgi.debug = 1

alias.url = (
    "/static" => "/var/www/static"
)

My routes look like:

PAGE = Blueprint("home", __name__)

@PAGE.route("/page1", methods=["GET"])
def page1_view():
    ...

@PAGE.route("/foo/page2", methods=["GET"])
def page2_view():
    ...

And finally the blue print registering:

app = Flask(__name__)

app.register_blueprint(PAGE)

Solution

  • Turns out "/" as a fastcgi route will match anything (e.g., will match the seconds "/" in "/foo/").

    The fix is to change the fastcgi.server directive to:

    $HTTP["url"] !~ "^/static" {
        fastcgi.server = ("" =>
            ((
                "socket" => "/tmp/foobar-fcgi.sock",
                "bin-path" => "/home/foobar/app.fcgi",
                "check-local" => "disable",
                "max-procs" => 1
            ))
        )
    }