gunicornfastcgilighttpd

Cannot get Lighttpd connecting with a Gunicorn backend server


I'm following this (very good) tutorial on how to serve a Flask app over hte intenet. I now have everything working well using Nginx - but I want to use Lighttpd with Fastcgi as that is my main web server and is the one I understand best. Unfortunately I cannot get it running with Lighttpd. I have used this Lighttpd FastCGI guide, specifically the Example with Prefix example as what I want to achieve is a call to: www.example.com/flask but I'm getting an 500 Internal Server Error when I enter localhost/flask on my development system (Mint Linux running in a Windows VirtualBox)

The server directive in lighttpd.conf is:

fastcgi.server = (
  "/flask" =>
  (( "socket" => "/home/dconran/python/dg/myproject.sock",
     "check-local" => "disable",
  ))
)

(I have included mod_fastcgi in the server.modules directive at the top of the conf file)

The equivalent location directive in Nginx is:

location ^~ /flask/ {
    rewrite ^/flask(/.*)$ $1 break;    
    include proxy_params;
    proxy_pass http://unix:/home/dconran/python/dg/myproject.sock;
}

The systemd.service code to place the Gunicorn backend server running in the background is:

Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=dconran
Group=www-data
WorkingDirectory=/home/dconran/python/dg
Environment="PATH=/home/dconran/python/dg/venv/bin"
ExecStart=/home/dconran/python/dg/venv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target

and the error message being reported by journalctl is:

Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/parser.py", line 41, in __next__
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     self.mesg = self.mesg_class(self.cfg, self.unreader, self.source_addr, self.req_count)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/message.py", line 259, in __init__
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/message.py", line 259, in __init__
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     super().__init__(cfg, unreader, peer_addr)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/message.py", line 60, in __init__
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     unused = self.parse(self.unreader)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:              ^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/message.py", line 274, in parse
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     line, rbuf = self.read_line(unreader, buf, self.limit_request_line)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/message.py", line 326, in read_line
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     self.get_data(unreader, buf)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/message.py", line 262, in get_data
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     data = unreader.read()
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:            ^^^^^^^^^^^^^^^
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/unreader.py", line 36, in read
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     d = self.chunk()
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:         ^^^^^^^^^^^^
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/http/unreader.py", line 63, in chunk
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     return self.sock.recv(self.mxchunk)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:   File "/home/dconran/python/dg/venv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 204, in handle_abort
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]:     sys.exit(1)
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]: SystemExit: 1
Feb 16 13:16:58 dconran-VirtualBox gunicorn[30023]: [2025-02-16 13:16:58 +0000] [30023] [INFO] Worker exiting (pid: 30023)

Like I say, I want to be able to enter localhost/flask into a browser accessing a Lighttpd web server and get back the Flask output. This is working in Nginx but not in Lighttpd. What am I doing wrong?


Solution

  • rewrite ^/flask(/.*)$ $1 break; for nginx suggests that your backend should understand the flask/ prefix for routing, but my guess is that is where your app is throwing an error.

    As an alternative to lighttpd mod_fastcgi, you can use lighttpd mod_proxy with

    proxy.header "map-urlpath"

    See https://wiki.lighttpd.net/mod_proxy