【问题标题】:django wsgi with apache2 configuration gives page not found 404具有 apache2 配置的 django wsgi 给出页面未找到 404
【发布时间】:2021-06-30 00:03:16
【问题描述】:

这里假设

testsite.com 这是我的 php 应用程序和

testsite.com/project 是 python-django 应用程序

我的 apache 站点配置文件 /etc/apache2/sites-available/site.conf 中有以下设置

<VirtualHost *:443>
                ServerAdmin webmaster@testsite.com
                ServerName testsite.com
                DocumentRoot /var/www/html

                WSGIDaemonProcess test python-path=/var/www/html/projectenv/test-project python-home=/var/www/html/projectenv
                WSGIProcessGroup test
                WSGIScriptAlias /project /var/www/html/projectenv/test-project/test-project/wsgi.py


                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

注意:我必须将 WSGIScriptAlias 保留为 /project,因为只有这个 url 我的 django 项目才能工作。

但是当我点击testsite.com/project 时,它会出现以下错误

Page not found (404)
Using the URLconf defined in test-project.urls, Django tried these URL patterns, in this order:

admin/
project/
The empty path didn't match any of these.

这是我的项目 url 结构

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('projectapp.urls'))
]

projectapp/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('project/', views.home, name='project'),
]

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_filters',
    'projectapp',

]

请提出解决方案,当我点击这个 url testsite.com/project 它应该通过我定义的 django 应用程序并呈现链接到这个视图的页面,它观察到 wsgi 无法通过 django 项目结构并且由于它没有识别应用程序中给出的 url。或者可能是它无法找到应用程序结构。

为了使这项工作我应该改变什么,请提出建议

【问题讨论】:

  • 如果您使用testsite.com/project,它将在testsite.com 目录中查找project,因为它是一个不同的项目,所以它不会存在。您最好使用project 作为子域。 IE。 project.testsite.com
  • @Anthony:testsite.com 不是目录。是我的网站域,所以每当这个 url 命中它通过 virtualhost 中提到的文档根目录时,我建议请在site.conf 文件中仔细检查我的项目结构我想使用 url /project 来呈现我的django 应用程序。所以请相应地提出建议

标签: python django apache apache2


【解决方案1】:

终于找到了解决办法,有点概念性

由于WSGIScriptAlias上面提到/project,这意味着对于wsgi,项目范围将仅限于/project目录,而/project目录之外的其他应用程序将无法访问wsgi

这就是为什么它给Page not found (404)

因此这是它所期望的如下

WSGIScriptAlias /test-project /var/www/html/projectenv/test-project/test-project/wsgi.py

所以现在 /test-project 中的所有应用程序 url 都可以被 wsgi 访问

我现在可以使用testsite.com/test-project/projecttestsite.com 访问我的django 项目网址,这是我的php 应用程序,两者都运行得非常好。

所以这里的教训是,wsgi 总是需要主项目目录路径来遍历它里面的所有应用程序,虽然它在 django 官方文档中提到但我们通常会忽略这个概念。希望这个答案能帮助遇到同样问题的你!

【讨论】:

    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2021-07-29
    • 1970-01-01
    • 2013-04-07
    • 2015-12-09
    相关资源
    最近更新 更多