簡體   English   中英

將項目根 url 設置為非“/”時,Django 項目不起作用

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

我在 Linux 服務器上有一個 Django 項目(使用 Django、uwsgi 和 Nginx 來部署它),當我在 Nginx 中將項目根 url 設置為

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

因此可以使用如下網址訪問該項目: www.mysite.com

但是,我計划在此服務器上構建更多 Web 項目,並希望使用不同的 url 訪問它們,例如: www.mysite.com/oldproject www.mysite.com/project2```

所以我將我現在現有項目的 Nginx conf 文件更改為:

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

但是,當我使用www.mysite.com/oldproject訪問舊項目時,我得到的反饋如下:未找到。 無法獲取服務器上的資源

我很確定 Nginx 與 uwsgi 運行良好,因為我做了一些簡單的測試,所以,出了什么問題?

完整配置如下圖所示:

Nginx 配置:

# 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 配置:

[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

錯誤日志:

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)
'''

您應該向 Django 網址模式添加前綴“/oldproject”。

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

或者

from django.conf import settings

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

如果要配置前綴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM