How to redirect URLs without break a FastCGI app

fastcgilighttpdredirect

(I consider this question a duplicate of Lighttpd redirect from
www.domain.com to domain.com
, but that one didn't get enough
attention and it's too old).

I'm trying to deploy an app over lighttpd+FastCGI and encrypt all
the traffic. It works well if I explicitly use HTTPS in the URL,
but as soon I try the redirect from HTTP to HTTPS the URLs the app
script name (in this case, index.py) is included in the URL, so
instead of https://somedomain.com/bleh I get
https://somedomain.com/index.py/bleh, which triggers a Not Found error.

I tried moving some stuff around, but I can't get how to do the
redirect well. Here's the relevant stuff of my lighttpd.conf

$SERVER["socket"] == ":80" {
    $HTTP["host"] =~ "(.*)" {
        url.redirect = (
            "^/(.*)" => "https://%1/$1"
        )
    }
}

$SERVER["socket"] == ":443" {
    ssl.engine = "enable"
    ssl.pemfile = "certificate.pem"
    ssl.use-sslv2 = "disable"
    ssl.use-sslv3 = "disable"
}

fastcgi.server = (
    "index.py" => ((
        "socket" => "/tmp/app.socket",
        "bin-path" => "index.py",
        "max-procs" => 1,
        "bin-environment" => (
            "REAL_SCRIPT_NAME" => ""
        ),
        "check-local" => "disable"
    ))
)

url.rewrite-once = (
    "^/favicon.ico$" => "/static/assets/favicon.ico",
    "^/static/(.*)$" => "/static/$1",
    "^/(.*)$" => "/index.py/$1"
)

Best Answer

rewrite happens before redirect. in your case the solution is to put fastcgi and rewrite in the ssl socket, as you only want it for ssl anyway.

Please don't spawn sockets in /tmp, use a directory dedicated to this where only lighttpd can create files.