Django gunicorn nginx forward to< domain>/app

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

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

  1. django_project
  2. ||
  3. bjsite
  4. -- settings.py
  5. -- wsgi.py
  6. -- ...
  7. ||
  8. bj
  9. -- models.py
  10. -- views.py
  11. -- templates
  12. -- ...
  13. ||
  14. manage.py
  15. static
  16. requirements.txt
  17. venv
  18. ...

在settings.py中,这是我的WSGI_APPLICATION = 'bjsite.wsgi.application'
海雀服务

  1. [Unit]
  2. Description=gunicorn daemon
  3. Requires=bj.socket
  4. After=network.target
  5. [Service]
  6. User=bj
  7. Group=www-data
  8. WorkingDirectory=/home/bj/bjsite
  9. ExecStart=/home/bj/bjsite/venv/bin/gunicorn \
  10. --access-logfile /home/bj/bjsite_deploy/gunicorn_access.log \
  11. --error-logfile /home/bj/bjsite_deploy/gunicorn_error.log \
  12. --workers 3 \
  13. --bind unix:/run/bj.sock \
  14. bjsite.wsgi:application
  15. [Install]
  16. WantedBy=multi-user.target

和我的nginx cong在sites-available

  1. server {
  2. listen 80;
  3. server_name domain.de www.domain.de;
  4. location = /favicon.ico { access_log off; log_not_found off; }
  5. location / {
  6. include proxy_params;
  7. proxy_pass http://unix:/run/bj.sock;
  8. }
  9. location /static/ {
  10. root /home/bj/bjsite_deploy;
  11. }
  12. location /media/ {
  13. root /home/bj/bjsite_deploy;
  14. }
  15. listen 443 ssl; # managed by Certbot
  16. ssl_certificate /etc/letsencrypt/live/domain.de/fullchain.pem; # managed by Certb>
  17. ssl_certificate_key /etc/letsencrypt/live/domain.de/privkey.pem; # managed by Cer>
  18. include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  19. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  20. }
  21. server {
  22. if ($host = www.domain.de) {
  23. return 301 https://$host$request_uri;
  24. } # managed by Certbot
  25. if ($host = domain.de) {
  26. return 301 https://$host$request_uri;
  27. } # managed by Certbot
  28. server_name domain.de www.domain.de;
  29. listen 80;
  30. return 404; # managed by Certbot
  31. }

当我在浏览器中添加请求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中,我只需要指定页面的路由,而不需要使用斜杠:

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

相关问题