lighttpd mod_rewrite vs. apache mod_rewrite with Django and FastCGI
-
12-09-2019 - |
Question
I am in the process of migrating from a Django FastCgi setup in Apache to one in lighttpd.
On Apache, I was using the fcgi config described in the Django docs. The core part being rewriting all my non-static URLs to be /mysite.fcgi/$1:
RewriteRule ^/(.*)$ /mysite.fcgi/$1 [QSA,L]
and then forwarding all requests for /mysite.fcgi for FastCGI:
<IfModule mod_fastcgi.c>
FastCGIExternalServer /opt/www/mysite.fcgi -host 127.0.0.1:8000
</IfModule>
The setup worked for Django. If, for example, I went to http://www.mydomain.com/help/
and I printed {{ request.get_full_path }}
in the template, the result was /help/
. Life was good and I was happy. However, I ran into some issues which are forcing me to move to a web server which support more simultaneous connections than Apache can give me.
Fast forward to lighttpd. All is configured and well. I am rewriting my URLs with mod_rewrite:
url.rewrite-once = ( "^(/media/.)$" => "$1", "^/favicon.ico$" => "/med/img/favicon/favicon.ico", "^(/.)$" => "/mysite.fcgi$1", )
and have FastCGI handling /mysite.fcgi:
fastcgi.server = (
"/mysite.fcgi" => (
"main" => (
"host" => "127.0.0.1",
"port" => 8000,
"check-local" => "disable",
)
),
)
Things work in general and my Django site runs fine. BUT when I go to http://www.mydomain.com/help/
and print {{ request.get_full_path }}
in the template, the result is /mysite.fcgi/help/
. This causes some issues.
While this does not cause problems in general as the Django site works well, it does cause issues when I use SSL. Specifically, I use sslmiddleware from "Stephen Zabel - sjzabel@gmail.com" from http://www.djangosnippets.org/snippets/240/ . This software relies on request.get_full_path, which returns a different value under lighttpd than it did under Apache. Same for request.path .
Can anyone suggest a way out of this problem? Ideally, I would like lighttpd mod_rewrite to have the same behavior as mod_rewrite under Apache. If not possible, I would like a lighttpd FastCGI Django setup which would be compatible with the sslmiddleware package I am using. Alternatively, I could change the sslmiddleware package to be compatible with the way which mod_rewrite from lighttpd rewrites URLs.
This answer would make a fantastic 30th birthday present!
Solution
Did you try to define FORCE_SCRIPT_NAME to "" in settings.py and restart the fastcgi server?
FORCE_SCRIPT_NAME=""
OTHER TIPS
See http://redmine.lighttpd.net/issues/show/729 if you'd rather fix it in lighttpd.
In short, upgrade to 1.4.23 and add "fix-root-scriptname" => "enable" to the fcgi config.