简体   繁体   中英

Django project doesn't work when setting project root url to not “/”

I had a Django project (using Django, uwsgi and Nginx to deploy it) on a linux server that works well when I'm setting the project root url in Nginx as

    location / {
        uwsgi_pass  django;
        include     /path/to/project/uwsgi_params;
    }

So the project can be accessed using url like: www.mysite.com

However, I'm planing build more web projects on this server, and want to visit them using different urls like: www.mysite.com/oldproject www.mysite.com/project2```

So I changed my now existing project's Nginx conf file as:

    location /oldproject {
        uwsgi_pass  django;
        include     /path/to/project/uwsgi_params; 
    }

however, when I visit the old project using www.mysite.com/oldproject I get feed back like: Not Found. cant get resource on the server

I'm pretty sure that the Nginx is working well with uwsgi because I did some simple test, so, what is going wrong?

The full configuration is shown below:

Nginx conf:

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///web/welib/welib.sock; # for a file socket
}

# configuration of the server
server {

    listen       443 ssl;
    ssl_certificate /etc/nginx/certif/1_www.xxxxxxx.crt;
    ssl_certificate_key /etc/nginx/certif/2_www.xxxxx.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE8-GCM-SHA256:ES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;  

    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    #log config
    access_log      /path/to/project/log/access/nginx_access.log;
    error_log       /path/to/project/error/nginx_error.log;




    location /oldproject/static {
        alias /path/to/project/static/; 
    }

    location /oldproject {
        uwsgi_pass  django;
        include     /path/to/project/uwsgi_params; 
    }

}

uwsgi conf:

[uwsgi]

#used socket
#socket = 127.0.0.1:9090
socket = /path/to/project/myproject.sock

#base to Django file
chdir = /path/to/project/welib

#Django's wsgi file
module = myproject.wsgi

#process related settings
master = true

#Django's wsgi file (for test use)
##wsgi-file = /path/to/project/test.py

#path to virtualenv
#home = /web/web_env

#permission code
chmod-socket = 666

#clear enviroment on exit
vacuum = true

#log_file
daemonize = /web/welib/welib.log 

#http :8003

error log:

spawned uWSGI worker 1 (pid: 4369, cores: 1)
[pid: 4369|app: 0|req: 1/1] 36.5.192.216 () {40 vars in 636 bytes} [Tue Aug 25 02:56:53 2020] GET /welib/ => generated 179 bytes in 92 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 2/2] 36.5.192.216 () {40 vars in 636 bytes} [Tue Aug 25 02:56:54 2020] GET /welib/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 3/3] 36.5.192.216 () {40 vars in 652 bytes} [Tue Aug 25 02:57:00 2020] GET /welib/allbooks => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 4/4] 36.5.192.216 () {40 vars in 654 bytes} [Tue Aug 25 02:59:34 2020] GET /welib/allbooks/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 5/5] 36.5.192.216 () {40 vars in 652 bytes} [Tue Aug 25 02:59:41 2020] GET /welib/allbooks => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 6/6] 36.5.192.216 () {40 vars in 636 bytes} [Tue Aug 25 02:59:43 2020] GET /welib/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 7/7] 36.5.192.216 () {40 vars in 652 bytes} [Tue Aug 25 02:59:43 2020] GET /welib/allbooks => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 8/8] 36.5.192.216 () {40 vars in 646 bytes} [Tue Aug 25 02:59:44 2020] GET /welib/admin => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 9/9] 36.5.192.216 () {40 vars in 648 bytes} [Tue Aug 25 03:00:05 2020] GET /welib/admin/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 10/10] 36.5.192.216 () {40 vars in 646 bytes} [Tue Aug 25 03:00:07 2020] GET /welib/admin => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 11/11] 36.5.192.216 () {40 vars in 636 bytes} [Tue Aug 25 03:00:08 2020] GET /welib/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 12/12] 36.5.192.216 () {40 vars in 634 bytes} [Tue Aug 25 03:00:08 2020] GET /welib => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 13/13] 36.5.192.216 () {40 vars in 636 bytes} [Tue Aug 25 03:00:09 2020] GET /welib/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 14/14] 36.5.192.216 () {40 vars in 636 bytes} [Tue Aug 25 03:00:10 2020] GET /welib/ => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 15/15] 36.5.192.216 () {40 vars in 634 bytes} [Tue Aug 25 03:20:09 2020] GET /welib => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
[pid: 4369|app: 0|req: 16/16] 36.5.192.216 () {40 vars in 634 bytes} [Tue Aug 25 03:20:09 2020] GET /welib => generated 179 bytes in 1 msecs (HTTP/1.1 404) 4 headers in 128 bytes (1 switches on core 0)
'''

You should add prefix '/oldproject' to the Django urls patterns.

urlpatterns = [path(r'^oldproject/', include(urlpatterns))]

or

from django.conf import settings

if settings.BASE_URL_PREFIX:
    urlpatterns = [path(settings.BASE_URL_PREFIX, include(urlpatterns))]

if you want to configure prefix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM