You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django+Apache+mod_wsgi部署:非首页URL返回403权限拒绝问题

问题分析与解决方案

核心问题

  1. Apache Alias与WSGIScriptAlias冲突:配置中Alias /dashboardteste/指向项目目录,Apache会优先尝试从该目录查找静态文件,而非将请求转发给Django处理,导致子路径(如/dashboardteste/pop)返回403。
  2. Directory权限配置不足:当前仅允许wsgi.py文件的访问,未授权Apache处理该目录下的动态请求转发。
  3. 路由与访问路径不匹配:urls.py中path('pop', ...)无尾部斜杠,若访问的是/pop/(带斜杠)且未使用挂载前缀/dashboardteste,会导致路径不匹配。

具体修复步骤

1. 修改Apache配置文件(.conf)

移除冲突的Alias配置,调整Directory权限以支持WSGI请求转发:

<VirtualHost *:443>
ServerAdmin [CENSORED]
DocumentRoot [CENSORED]
ServerName [CENSORED]
ServerAlias [CENSORED]

RewriteEngine On
RewriteCond [CENSORED]
RewriteCond [CENSORED]
RewriteRule [CENSORED]

# 移除冲突的Alias配置
# Alias /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/

# 以apache用户运行Django
SuexecUserGroup apache apache
#Header set X-Frame-Options SAMEORIGIN

# 调整目录权限,允许Apache处理WSGI请求
<Directory /home/andremou/DashboardUnicamp/unicamp/unicamp>
    Require all granted
    Options FollowSymLinks
    AllowOverride None
</Directory>

# TLS/SSL证书配置
SSLEngine on
SSLCertificateFile /etc/httpd/conf.d/ssl/moodle.pem
SSLCertificateKeyFile /etc/httpd/conf.d/ssl/moodle.key
SSLCertificateChainFile /etc/httpd/conf.d/ssl/moodle_chain.crt

WSGIScriptAlias /dashboardteste /home/andremou/DashboardUnicamp/unicamp/unicamp/wsgi.py
WSGIDaemonProcess unicamp python-home=/home/andremou/DashboardUnicamp/my_env python-path=/home/andremou/DashboardUnicamp/unicamp
WSGIProcessGroup unicamp

WSGIApplicationGroup %{GLOBAL}
</VirtualHost>

2. 修正Django路由(urls.py)

将pop路由改为带尾部斜杠的形式,匹配常规访问路径:

from django.contrib import admin
from django.urls import path, include
from dashboard import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.NewView.as_view()),
    # 添加尾部斜杠,匹配带斜杠的访问路径
    path('pop/', views.PopView.as_view()),
]

urlpatterns += staticfiles_urlpatterns()

3. 使用正确的访问路径

由于WSGIScriptAlias挂载在/dashboardteste,正确的访问路径应为:

  • 首页:https://你的域名/dashboardteste/
  • Pop页面:https://你的域名/dashboardteste/pop/

4. 修复文件权限

确保apache用户拥有项目目录的读取权限:

chown -R apache:apache /home/andremou/DashboardUnicamp/unicamp
chmod -R 755 /home/andremou/DashboardUnicamp/unicamp

5. 重启Apache服务

systemctl restart httpd

内容的提问来源于stack exchange,提问作者André Mouro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 23:52:42