2

I'm trying to setup django under some suburl, lets say /myproject, with nginx and uwsgi. However, I can't get it working. Whatever I try, seems that uwsgi_modifier1 30; option doesn't work. I always get doubled path, instead of localhost:8000/myproject, I get localhost:8000/myproject/myproject

What am I missing? Here are the relevant files:

Django urls.py

from django.conf.urls import patterns, include, url
from django.http import HttpResponse

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', lambda x: HttpResponse('Hello world'), name='home'),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

I didn't change anything in default django settings.py, except adding DB info. And here is nginx conf file:

upstream mydjango {
    server unix:///home/username/www/myproject/c.sock;
}

server {
    listen 8000;
    server_name localhost;

    location /myproject/ {
        uwsgi_pass mydjango;
        include /home/username/www/myproject/uwsgi_params;
        uwsgi_param SCRIPT_NAME /myproject;
        uwsgi_modifier1 30;
    }
}

and I'm starting uwsgi from command line for now with:

uwsgi --socket c.sock --module myproject.wsgi --chmod-socket=666

I don't find any errors in the logs, just 404, because there is no nginx conf for path / on 8000 port, but there is no django url rule to match /myproject/myproject/ either. So where is my mistake? If that is relevant, I'm trying this on debian wheezy, nginx the latest from mainline, python-3.3.2

2

Have you tried using rewrite instead of uwsgi_modifier1 ?

...
    location /myproject {
        rewrite /myproject(.*) $1 break;
        include /home/username/www/myproject/uwsgi_params;
        uwsgi_pass mydjango;
    }
...
2
  • This seems to work fine, any ideas why uwsgi_modifier1 doesn't work? – Rapolas K. Aug 27 '13 at 20:02
  • @RapolasK. No idea, I have never used this directive. – Changaco Aug 27 '13 at 20:15
1

I got it working! Trick was to tell Django about the path with FORCE_SCRIPT_NAME as well and modify static paths. For me this solution is good enough as suburl is only configured in local settings for Django and in nginx.conf.

Ubuntu 14.04 + Django 1.8 + uwsgi 1.9.17.1 + nginx 1.4.6

nginx.conf:

server {
    listen 80;
    server_name 192.168.1.23 firstsite.com www.firstsite.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /1/static {
        root /home/ubuntu/firstsite;
    }

    location /1 {
        include         uwsgi_params;
        uwsgi_param SCRIPT_NAME /1;
        uwsgi_modifier1 30;
        uwsgi_pass      unix:/home/ubuntu/firstsite/firstsite.sock;
    }
}

Add three lines to Django firstsite/settings.py:

FORCE_SCRIPT_NAME = '/1'
ADMIN_MEDIA_PREFIX = '%s/static/admin/' % FORCE_SCRIPT_NAME
STATIC_URL = '%s/static/' % FORCE_SCRIPT_NAME

For the completeness, here is my uwsgi firstsite.ini using virtualenv in ~home/Env:

[uwsgi]
project = firstsite
base = /home/ubuntu

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.