将项目根 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;
}
因此可以使用 url 访问该项目,例如:
www.mysite.com
但是,我计划在此服务器上构建更多 Web 项目,并希望使用不同的 urls 来访问它们,例如:
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))]
如果要配置前缀
我在 linux 服务器上有一个 Django 项目(使用 Django、uwsgi 和 Nginx 来部署它),当我在 Nginx 中设置项目根目录 url 时,它运行良好
location / {
uwsgi_pass django;
include /path/to/project/uwsgi_params;
}
因此可以使用 url 访问该项目,例如: www.mysite.com
但是,我计划在此服务器上构建更多 Web 项目,并希望使用不同的 urls 来访问它们,例如: 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))]
如果要配置前缀