大网数据广告活动
服务器运维问答

nginx django suburl doesn't work

· 52CE 我爱测

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 …

问题原文

原帖链接 · Rapolas K. · CC BY-SA 3.0

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

已采纳解答

原帖链接 · Changaco · CC BY-SA 3.0

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;
    }
...

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-08-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。