title: Run django app via nginx+uwsgi in a subpath
body: <p>I want to run a simple test project in a subdirectory alias on our development server. The basic setup is an nginx with a location that passes everything in a subdirectory to the wsgi application.</p>
  
  <p>Django obviously does not understand that it runs in an subdirectory alias, which completely destroys URL generation and parsing.<br>
  I could not find any prefix-like setting in the docs and my google fu also did not help that much... so I'm asking here instead.</p>
  
  <p>The only thing I did find was the setting FORCE_SCRIPT_NAME which at least fixes the URL generation. (see: <a href="http://docs.webfaction.com/software/django/config.html#mounting-a-django-application-on-a-subpath">http://docs.webfaction.com/software/django/config.html#mounting-a-django-application-on-a-subpath</a>)<br>
  Sadly this does not fix the urlconf parsing, even though the mentioned site suggests that.</p>
  
  <p>Is it possible to run a django application in a subdirectory alias and if so, how?</p>
  
  <p>nginx config:</p>
  
  <pre><code>server {
          location /fancyprojectname/static {
                  alias /srv/fancyprojectname/static;
          }
  
          location /fancyprojectname/ {
                  uwsgi_pass unix://var/run/uwsgi/app/fancyprojectname/socket;
                  include uwsgi_params;
          }
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>So, setting "uwsgi_param SCRIPT_NAME /fancyprojectname;" in the nginx location makes FORCE_SCRIPT_NAME unnecessary - sadly the URL matching still does not work.</p>
  
  <pre><code>from django.conf.urls import patterns, include, url
  
  # Uncomment the next two lines to enable the admin:
  from django.contrib import admin
  admin.autodiscover()
  
  urlpatterns = patterns('',
      # Uncomment the admin/doc line below to enable admin documentation:
      # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
  
      # Uncomment the next line to enable the admin:
      url(r'^admin/', include(admin.site.urls)),
  )
  </code></pre>
  
  <p>What I think what is happening: As the admin regex starts with "^admin" and the actual URL is "fancyprojectname/admin/", Django can't match the URLs correctly, even though the SCRIPT_NAME is set.</p>
  
  <p><strong>The solution</strong></p>
  
  <p>So, it was indeed a problem with SCRIPT_NAME.</p>
  
  <p>The <a href="http://www.python.org/dev/peps/pep-0333/">WSGI specification</a> says the following:</p>
  
  <blockquote>
    <p>SCRIPT_NAME
        The initial portion of the request URL's "path" that corresponds to the application object, so that the application knows its virtual
    "location". This may be an empty string, if the application
    corresponds to the "root" of the server.  </p>
    
    <p>PATH_INFO
    The remainder of the request URL's "path", designating the virtual "location" of the request's target within the application. This may be
    an empty string, if the request URL targets the application root and
    does not have a trailing slash.</p>
  </blockquote>
  
  <p>Nginx does not set SCRIPT_NAME automatically, so this needs to be set in any case. Afterwards PATH_INFO is wrong, because in the default setup Nginx sets this to $document_uri, which would be the full URL.</p>
  
  <p>"uwsgi_modifier1 30;" tells Nginx to set UWSGI_MODIFIER_MANAGE_PATH_INFO, which in turn tells UWSGI to strip off the SCRIPT_NAME of the PATH_INFO.</p>
  
  <p>The combination of these settings seem to work, as Django can now generate AND match URLs correctly.</p>