Django + uWSGI + nginx url mapping Django + uWSGI + nginx url mapping nginx nginx

Django + uWSGI + nginx url mapping


I had trouble to understand your question but basically it resumes to this: I want nginx to pass all calls to /api/ to django but django is receiving the URL with /api/ prefixed.


nginx trick (does not work for internal URLs)

I'd use nginx's rewrite to solve this

location /api/ {    rewrite ^/api/(.*) /$1  break;    include /vagrant/api/uwsgi_params;    uwsgi_pass testing;}

It shall drop the /api/ part of the URL and remain in the block to uwsgi_pass into Django.


django solution (dirty in my opinion)

The above works for the first URL going into django. Yet, django is clueless about the prefix that was removed and will have trouble to generate URLs to itself. That might not be a problem in some simple APIs but it would be a problem more often than not.

I guess, the only viable way is to add the prefix to the base URLconf.

from django.conf.urls import includemypatterns = [    url(r'^admin/', admin.site.urls),]urlpatterns = [    url(r'^api/', include(mypatterns)),]

Although, I feel this is a very dirty solution.