Home > Software engineering >  Problem serving Django "polls" tutorial under lighttpd: 404 page not found
Problem serving Django "polls" tutorial under lighttpd: 404 page not found

Time:01-27

I am following the Django polls tutorial, which is working 100% with the built-in development server (python3 manage.py runserver).

I have set up lighttpd to serve django through UWSGI and that seems to be working fine but for one glitch: the URL passed to django seems to have been modified.

My lighttpd configuration is basically this:

...
server.modules  = ("mod_scgi","mod_rewrite")
scgi.protocol = "uwsgi"
scgi.server   = (
    "/polls" => ((
             "host" => "localhost",
             "port" => 7000,
             "check-local" => "disable",
    ))
)

The Django tutorial mapping looks like:

# tutorial1/urls.py
urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

# polls/urls.py
app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

However when I hit http://localhost:8080/polls/ in the address bar, it produces an 404 error.

enter image description here

If I add an extra /polls to the URL then it works just fine.

enter image description here

enter image description here

My goal with this exercise is to be able to serve this app switching from and to both servers without needing to modify configuration files each time.

What do I need to do on the lighttpd.conf side to make lighttpd interchangeable with Django's own internal dev server?

I have tried to add the following url.rewrite rule but it messes up completely the URL handling.

url.rewrite = (
    "^/polls/(.*)$" => "/polls/polls/$1"
)

With the URL rewrite, the first call http://127.0.0.1/polls/ works fine but the links to the detail posts are generated as http://127.0.0.1/polls/polls/1 and the resulting sub-url polls/1 is not recognized by the polls/urls.py settings.

Using the empty path in lighttpd server:

    scgi.protocol = "uwsgi"
    scgi.server   = (
        "" => ((
                 "host" => "localhost",
                 "port" => 7000,
                 "check-local" => "disable",
        ))
    )

Produces this very weird result: enter image description here

The Raised by: polls.views.DetailView means that the URL is actually being parsed and forwarded correctly to the Polls Detail view, which is 100% the correct handler, but it is somehow raising a 404? WTF

Thank you!

CodePudding user response:

As stated here

The setting of the variable fix-root-scriptname is required for some versions of Lighttpd to fix a bug in Lighttpd when it is running a SCGI server rooted at /.

scgi.server   = (
    "/" => ((
             "host" => "localhost",
             "port" => 7000,
             "check-local" => "disable",
             "fix-root-scriptname" => "enable",
    ))

Previous answer:

Have you tried using an empty string? (It's a not a proper answer to the question but I can't make a comment)

  • Related