我如何使用Django在nginx中设置子目录 [英] How do I set subdirectory in nginx with Django

查看:314
本文介绍了我如何使用Django在nginx中设置子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:


  • uwsgi

  • nginx

  • django 1.3

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

I'm using the domain www.example.com with Django and nginx, and I want to access the Django by www.example.com/abc/ , but I don't know how to set the subdirectory.

这是nginx conf文件:

This is the nginx conf file:

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 $

When I open the website www.example.com/abc/, the django urls.py doesn't match, it only match the site like ^index$.

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

How can I modify the nginx location to set django to www.example.com/abc?

推荐答案

根据 uWSGI on Nginx docs,你只需要将 SCRIPT_NAME 传递给django。

According to the uWSGI on Nginx docs, you just have to pass the SCRIPT_NAME to django.

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

Django仍然会看到 / abc ,但它应该处理它,以便在您的网址匹配之前被剥离。你想要这样做,如果django没有看到 / abc ,它会为您的网站生成不正确的网址,并且您的链接都不会工作。

Django will still "see" /abc, but it should deal with it so that it gets stripped off before your urls are matched. You want this to happen, if django didn't see /abc, it would generate incorrect urls for your site and none of your links would work.

这篇关于我如何使用Django在nginx中设置子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆