提问者:小点点

如何使用Django在nginx中设置子目录


环境:

  • uwsgi
  • nginx
  • django 1.3

我正在使用域www.example。com与Django和nginx,我想通过www.example访问Django。com/abc/,但我不知道如何设置子目录。

这是nginx conf文件:

server {
        listen 80;
        server_name www.example.com;
        error_log /var/log/nginx/xxx.error_log info;

        root /home/web/abc;  # this is the directory of the django program

        location ~* ^.+\.(jpg|jpeg|png|gif|css|js|ico){
                root /home/web/abc;
                access_log off;
                expires 1h;
        }

        location ~ /abc/ {   # I want to bind the django program to the domian's subdirectory
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:9000;
        }
}

当我打开网站www.example。com/abc/,djangoURL。py不匹配,它只匹配站点,如^index$

如何修改nginx位置以将django设置为www.example。com/abc


共2个答案

匿名用户

根据Nginx文档上的uWSGI,您只需将脚本\ u名称传递给django即可。

location /abc {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:9000;
    uwsgi_param SCRIPT_NAME /abc;            
}

Django仍将“看到”/abc,但它应该处理它,以便在匹配URL之前将其剥离。如果django没有看到/abc,它将为您的站点生成不正确的URL,并且您的链接将无法工作,您希望这种情况发生。

匿名用户

现在,Nginx和uwsgi的最新版本中删除了uwsgi\u modifier1 30,我不得不使用一种更新的方法来让它工作:

uWSGI配置:

[uwsgi]
route-run = fixpathinfo:

Nginx配置:

location /abc {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:9000;
    uwsgi_param SCRIPT_NAME /abc; # Pass the URL prefix to uWSGI so the "fixpathinfo:" route-rule can strip it out
}

如果这还不能解决问题:尝试安装libpcre和libpcre-dev,然后使用pip install-I--no cache dir-uwsgi重新安装uwsgi。uWSGI的内部路由子系统要求在编译/安装uWSGI之前安装PCRE库。更多关于uWSGI和PCRE的信息。