带有apache2配置的django-wsgi给出了找不到页面404



此处假设

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/uls.py

from django.urls import path
from . import views
urlpatterns = [
path('project/', views.home, name='project'),
]

设置.py

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

请建议解决方案,当我点击这个urltestsite.com/project时,它应该通过我定义的django应用程序并呈现链接到此视图的页面。它观察到,wsgi无法通过django项目结构,因此它无法识别应用程序中给定的url。或者可能是找不到应用程序结构。

为了使这项工作正常进行,我应该更改什么,请建议

终于找到了解决方案,它的概念

由于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

我现在可以访问我的django项目url,作为testsite.com/test-project/project,以及作为我的php应用程序的testsite.com,两者都运行得非常好。

所以这里的教训是,wsgi总是需要主项目目录路径来遍历其中的所有应用程序,尽管django官方文档中提到了这一点,但我们通常忽略了这个概念。希望这个答案能帮助那些在同一问题上挣扎的人们

最新更新