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

配置Apache反向代理后访问www.mycompany.com/testblog出现404错误

Apache反向代理配置404问题排查与修复

问题1:域名不匹配

你的虚拟主机配置中ServerName和ServerAlias指定的是website.com,但实际访问的域名是www.mycompany.com,Apache无法匹配到对应虚拟主机,导致代理规则未生效。修改配置中的域名:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName mycompany.com
ServerAlias www.mycompany.com
DocumentRoot /var/www/website.com
Redirect permanent / https://mycompany.com/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName mycompany.com
ServerAlias www.mycompany.com
# 保留原有SSL相关配置

问题2:代理目标地址错误

当前ProxyPass指向的https://website.com:8000/testblog存在两个问题:

  • Django开发服务器默认运行在HTTP协议的8000端口,而非HTTPS(除非你专门为8000端口配置了SSL证书)
  • 使用外部域名会导致不必要的DNS解析,应改用本地回环地址

修正代理规则:

ProxyPreserveHost On

ProxyPass /testblog http://127.0.0.1:8000/testblog
ProxyPassReverse /testblog http://127.0.0.1:8000/testblog

问题3:未启用必要Apache模块

确保mod_proxy和mod_proxy_http模块已启用,执行以下命令:

sudo a2enmod proxy proxy_http
sudo systemctl restart apache2

Django应用配置调整

  1. 在settings.py中添加允许访问的主机:
ALLOWED_HOSTS = ['mycompany.com', 'www.mycompany.com', '127.0.0.1']
  1. 若Django应用的URL前缀为/testblog,需确保URLconf中的路径正确,或设置:
FORCE_SCRIPT_NAME = '/testblog'

(注:设置FORCE_SCRIPT_NAME后,静态文件和媒体文件的路径也需要对应调整,可通过STATIC_URL = '/testblog/static/'等方式修改)

内容的提问来源于stack exchange,提问作者Bastion2

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 14:40:18