Project

General

Profile

[Solved] can't get conditional working

Added by leolivier 17 days ago

Hi, I have a daphne site which I want to reverse proxy using lighttpd. For this, I'm using a unix socket through sockproxy but urls starting with /static must be served by lighttpd directly.
I wrote 10 different config for doing that, none of them worked, eg:

server.document-root = "/" 
...
server.modules = ( "mod_sockproxy", "mod_accesslog" )
...

$HTTP["url"] =~ "^/static/" {
    server.document-root = "/app" 
}
else $HTTP["url"] =~ "" {
    sockproxy.server = ("/" =>
        ( "cousinsmatter socket" => (
            "socket" => "/var/run/cousinsmatter.socket" 
        ))
    )
}

if I hit .../members/, I get
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.596) === start of condition block ===
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.441) go prev global / $HTTP["url"] =^ "/static/" 
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.461) 2 global / $HTTP["url"] =^ "/static/" not available yet
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.384) 1 (uncached) result: unset (cond: global / $HTTP["url"] =^ "/static/")
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.384) 2 (uncached) result: unset (cond: global / $HTTP["url"] =~ "")
2024-04-15 09:14:36 2024-04-15 07:14:36: (connections.c.770) fd:7 rqst: GET /members/ HTTP/1.1
...
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.596) === start of condition block ===
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.527) $HTTP["url"] =^ "/static/" compare to /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (configfile-glue.c.384) 1 (uncached) result: false (cond: global / $HTTP["url"] =^ "/static/")
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.420) -- parsed Request-URI
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.422) Request-URI     : /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.424) URI-scheme      : http
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.426) URI-authority   : 127.0.0.1:8000
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.428) URI-path (clean): /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.430) URI-query       : 
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.509) -- logical -> physical
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.511) Doc-Root     : /
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.513) Basedir      : /
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.515) Rel-Path     : /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.517) Path         : /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.203) -- : No such file or directory
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.206) Path         : /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.208) URI          : /members/
2024-04-15 09:14:36 2024-04-15 07:14:36: (response.c.179) fd:7 resp: HTTP/1.1 404 Not Found
...

so it does NOT go through the proxy but if I hit .../static/cm_main/images/cousinades.png, then it goes to the static path: (Same if just remove the "else" in the conditional)
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.596) === start of condition block ===
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.441) go prev global / $HTTP["url"] =^ "/static/" 
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.461) 2 global / $HTTP["url"] =^ "/static/" not available yet
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.384) 1 (uncached) result: unset (cond: global / $HTTP["url"] =^ "/static/")
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.384) 2 (uncached) result: unset (cond: global / $HTTP["url"] =~ "")
2024-04-15 09:27:19 2024-04-15 07:27:18: (connections.c.770) fd:7 rqst: GET /static/cm_main/images/cousinades.png HTTP/1.1
...
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.596) === start of condition block ===
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.527) $HTTP["url"] =^ "/static/" compare to /static/cm_main/images/cousinades.png
2024-04-15 09:27:19 2024-04-15 07:27:18: (configfile-glue.c.384) 1 (uncached) result: true (cond: global / $HTTP["url"] =^ "/static/")
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.420) -- parsed Request-URI
...
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.430) URI-query       : 
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.509) -- logical -> physical
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.511) Doc-Root     : /app
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.513) Basedir      : /app
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.515) Rel-Path     : /static/cm_main/images/cousinades.png
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.517) Path         : /app/static/cm_main/images/cousinades.png
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.536) -- handling subrequest
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.538) Path         : /app/static/cm_main/images/cousinades.png
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.540) URI          : /static/cm_main/images/cousinades.png
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.542) Pathinfo     : 
2024-04-15 09:27:19 2024-04-15 07:27:18: (response.c.179) fd:7 resp: HTTP/1.1 200 OK
...

And if change for instance like that:

$HTTP["url"] =~ "^/static/" {
    server.document-root = "/app" 
}
$HTTP["url"] !~ "^/static" {
    sockproxy.server = ("/" =>
        ( "cousinsmatter socket" => (
            "socket" => "/var/run/cousinsmatter.socket" 
        ))
    )
}

I again get a 404 on /members/
2024-04-15 09:41:29 2024-04-15 07:41:28: (configfile-glue.c.596) === start of condition block ===
2024-04-15 09:41:29 2024-04-15 07:41:28: (configfile-glue.c.461) 2 global / $HTTP["url"] !~ "^/static" not available yet
2024-04-15 09:41:29 2024-04-15 07:41:28: (configfile-glue.c.384) 2 (uncached) result: unset (cond: global / $HTTP["url"] !~ "^/static")
2024-04-15 09:41:29 2024-04-15 07:41:29: (connections.c.770) fd:7 rqst: GET /members/ HTTP/1.1
...
2024-04-15 09:41:29 2024-04-15 07:41:29: (configfile-glue.c.596) === start of condition block ===
2024-04-15 09:41:29 2024-04-15 07:41:29: (configfile-glue.c.527) $HTTP["url"] =^ "/static/" compare to /members/
2024-04-15 09:41:29 2024-04-15 07:41:29: (configfile-glue.c.384) 1 (uncached) result: false (cond: global / $HTTP["url"] =^ "/static/")
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.420) -- parsed Request-URI
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.422) Request-URI     : /members/
...
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.509) -- logical -> physical
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.511) Doc-Root     : /
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.513) Basedir      : /
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.515) Rel-Path     : /members/
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.517) Path         : /members/
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.203) -- : No such file or directory
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.206) Path         : /members/
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.208) URI          : /members/
2024-04-15 09:41:29 2024-04-15 07:41:29: (response.c.179) fd:7 resp: HTTP/1.1 404 Not Found

and .../static/cm_main/images/cousinades.png works fine.

if I remove the condition on the proxy like that:

$HTTP["url"] =~ "^/static/" {
    server.document-root = "/app" 
}
#$HTTP["url"] !~ "^/static" {
    sockproxy.server = ("/" =>
        ( "cousinsmatter socket" => (
            "socket" => "/var/run/cousinsmatter.socket" 
        ))
    )
#}

then /members hits the proxy and it works fine but /static/... is also going through the proxy:
2024-04-15 10:00:43 2024-04-15 08:00:42: (gw_backend.c.2674) handling the request using sockproxy
2024-04-15 10:00:43 Not Found: /static/cm_main/css/cm_main.css
2024-04-15 10:00:43 2024-04-15 08:00:43: (gw_backend.c.2674) handling the request using sockproxy
2024-04-15 10:00:43 Not Found: /static/cm_main/js/cm_main.js
2024-04-15 10:00:43 Not Found: /static/cm_main/images/cousinades.png
2024-04-15 10:00:43 2024-04-15 08:00:43: (gw_backend.c.2674) handling the request using sockproxy
2024-04-15 10:00:43 Not Found: /static/cm_main/js/cm_main.js
2024-04-15 10:00:43 Not Found: /static/cm_main/images/cousinades.png
2024-04-15 10:00:44 Not Found: /static/cm_main/images/favicon.ico

What is the correct syntax for such a simple case???


Replies (6)

RE: can't get conditional working - Added by leolivier 16 days ago

thanks a lot @gstrauss, I got it working with mod_proxy (although I don't understand why changing from one kind of proxy to another can change the conditional evaluation but as I said, I am new to lighttpd and I don't know the internals...)

RE: [Solved] can't get conditional working - Added by gstrauss 15 days ago

@leolivier When someone knocks on your door, do you know who it is before looking? mod_sockproxy is a socket proxy and proxies the socket without looking. mod_proxy is an HTTP reverse proxy and looks at the HTTP before proxying. You're using a lighttpd config condition on an HTTP condition: the URL is part of the HTTP request. lighttpd can only handle that condition after expecting an HTTP request and matching that HTTP condition. mod_sockproxy does not care whether HTTP or not, and does not expect HTTP, and does not parse HTTP. If mod_sockproxy is going to handle the socket, then mod_sockproxy chooses to do so prior to HTTP processing. mod_proxy is an HTTP reverse proxy and, if configured, handles an HTTP request only after lighttpd has parsed an HTTP request.

RE: [Solved] can't get conditional working - Added by leolivier 15 days ago

Thanks @gstrauss, much clearer now

RE: [Solved] can't get conditional working - Added by leolivier 15 days ago

leolivier wrote in RE: [Solved] can't get conditional working:

Thanks @gstrauss, much clearer now

Btw, maybe I am very bad at searching but gI struggled a lot to understand how to setup a socket as I didn't find any documentation for reverse proxying using sockets. Adding such an example in the documentation would maybe reduce the number of questions...

RE: [Solved] can't get conditional working - Added by gstrauss 15 days ago

but gI struggled a lot to understand how to setup a socket as I didn't find any documentation for reverse proxying using sockets.

thank you for sharing your feedback. Perhaps it was confusing to you since mod_sockproxy was not a good fit for your use case, and you did not understand for what mod_sockproxy might be used. Maybe you should have looked to see if something else better matched your needs, e.g. mod_proxy. How did you find mod_sockproxy? What might have helped you find mod_proxy?

Adding such an example in the documentation would maybe reduce the number of questions...

Please avoid sharing unsubstantiated opinions with a data set consisting of a single user (you).

You wrote an unsubstantiated statement that it would "maybe" reduce the number of questions, and yet you did not find similar questions in your search? Curious.

    (1-6/6)