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

问题描述 投票:1回答:2

环境:

  • 乌斯吉
  • Nginx的
  • Django 1.3

我将网域www.example.com与Django和nginx结合使用,并且想通过www.example.com/abc/访问Django,但我不知道如何设置子目录

这是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/ ,django urls.py不匹配,它仅匹配^index$类的网站。

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

python django nginx uwsgi
2个回答
7
投票

根据Nginx文档uWSGI ,您只需要将SCRIPT_NAME传递给django。

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

Django仍然会“看到” /abc ,但是它应该处理它,以便在您的URL匹配之前将其剥离。 您希望这种情况发生,如果django没有看到/abc ,它将为您的站点生成错误的url,并且所有链接均不起作用。


0
投票

现在,在最新版本的Nginx和uWSGI中删除uwsgi_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的内部路由子系统要求在编译/安装uWSGI 之前先安装PCRE库。 有关uWSGI和PCRE的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.