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

Tan*_*Woo 1 python django nginx uwsgi

环境:

  • uwsgi
  • nginx的
  • django 1.3

我正在使用www.example.comDjango和nginx 的域,我想访问Django www.example.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;
        }
}
Run Code Online (Sandbox Code Playgroud)

当我打开网站时www.example.com/abc/,django urls.py不匹配,它只匹配网站就好^index$.

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

Nat*_*usa 7

根据关于Nginx文档的uWSGI,你只需要传递SCRIPT_NAME给django.

location /abc {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:9000;
    uwsgi_param SCRIPT_NAME /abc;            
}
Run Code Online (Sandbox Code Playgroud)

Django仍然会"看到" /abc,但它应该处理它,以便在你的网址匹配之前被剥离.您希望这种情况发生,如果django没有看到/abc,它会为您的网站生成不正确的网址,并且您的链接都不起作用.