我已经搜索了教程等,但不幸的是我仍然不明白如何将我的域的请求转发到特定的应用程序(而不是项目)。 这是我的项目结构
django_project
||
bjsite
-- settings.py
-- wsgi.py
-- ...
||
bj
-- models.py
-- views.py
-- templates
-- ...
||
manage.py
static
requirements.txt
venv
...
在settings.py中,这是我的
WSGI_APPLICATION = 'bjsite.wsgi.application'
独角兽服务
[Unit]
Description=gunicorn daemon
Requires=bj.socket
After=network.target
[Service]
User=bj
Group=www-data
WorkingDirectory=/home/bj/bjsite
ExecStart=/home/bj/bjsite/venv/bin/gunicorn \
--access-logfile /home/bj/bjsite_deploy/gunicorn_access.log \
--error-logfile /home/bj/bjsite_deploy/gunicorn_error.log \
--workers 3 \
--bind unix:/run/bj.sock \
bjsite.wsgi:application
[Install]
WantedBy=multi-user.target
以及我的 nginx cong 在可用站点中
server {
listen 80;
server_name domain.de www.domain.de;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/run/bj.sock;
}
location /static/ {
root /home/bj/bjsite_deploy;
}
location /media/ {
root /home/bj/bjsite_deploy;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.de/fullchain.pem; # managed by Certb>
ssl_certificate_key /etc/letsencrypt/live/domain.de/privkey.pem; # managed by Cer>
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.domain.de) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domain.de) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name domain.de www.domain.de;
listen 80;
return 404; # managed by Certbot
}
当我在浏览器中添加请求 www.domain.de/bj 时,一切正常。但我想将 www.domain.de 直接转发到应用程序。 在阅读了有关 wsgi 部署的 django 文档后,我认为这可能只是错误的 wsgi 应用程序可调用..?
wsgi 应用程序可调用错误?
gunicorn.service 中的工作目录错误?
nginx.conf 中的 proxy_pass 错误?
我错过了什么?很抱歉关于此事的第一百万个问题,但其他线程有不同的配置。我只有 Nginx - Gunicorn - Django 非常感谢
更新:实际上,解决方案更简单:在project.urls中,我只需指定不带斜杠的页面路由:
urlpatterns = [
path('', include("bj.urls", namespace='landing')),
path('admin/', admin.site.urls)
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)