Django gunicorn nginx forward to< domain>/app

cu6pst1q  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(136)

我已经搜索了教程和SO各地,但不幸的是,我仍然不明白如何转发请求我的域到一个特定的应用程序(而不是项目)。这是我的项目结构

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在sites-available

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转发到应用程序。在阅读了django关于wsgi部署的文档后,我想这可能是错误的wsgi应用程序可调用的。
1.可调用的wsgi应用程序错误?

  1. gunicorn.service中的工作目录错误?
  2. nginx.conf中的proxy_pass错误?
    我错过了什么?对不起,关于这个问题的第一百万个问题,但其他线程有不同的配置。我只是有Nginx - Gunicorn - Django非常感谢B
w80xi6nr

w80xi6nr1#

更新:实际上,解决方案更简单:在project.urls中,我只需要指定页面的路由,而不需要使用斜杠:

urlpatterns = [
    path('', include("bj.urls", namespace='landing')),
    path('admin/', admin.site.urls)
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

相关问题