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

Apache2反向代理配置问题:访问/ws路径出现404错误

解决Apache反向代理404问题的步骤

1. 确认代理模块已启用

首先检查Apache是否加载了必要的代理模块,执行以下命令:

a2enmod proxy proxy_http

执行后重启Apache服务:

systemctl restart apache2

2. 修正路径匹配的斜杠一致性

你的配置中ProxyPass "/ws" "http://<server2_IP>/",但访问的是/ws/,路径末尾的斜杠会导致匹配问题。调整为两种一致的写法:

  • 若要匹配/ws/开头的所有路径,改为:
ProxyPass "/ws/" "http://<server2_IP>/"
ProxyPassReverse "/ws/" "http://<server2_IP>/"
  • 或是去掉目标地址的斜杠,匹配/ws及后续所有路径:
ProxyPass "/ws" "http://<server2_IP>"
ProxyPassReverse "/ws" "http://<server2_IP>"

3. 给VirtualHost指定ServerName(确保配置生效)

即使在局域网环境,未设置ServerName可能导致Apache优先使用默认VirtualHost,而非你配置的规则。添加ServerName指向server1的IP:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName <server1_IP>
        DocumentRoot /var/www/html

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

        ProxyRequests off
        ProxyPreserveHost On
        ProxyPass "/ws/" "http://<server2_IP>/"
        ProxyPassReverse "/ws/" "http://<server2_IP>/"
</VirtualHost>

修改后重启Apache服务。

4. 验证server1到server2的网络连通性

在server1上执行命令测试能否正常访问server2:

curl http://<server2_IP>/

如果无法返回正常内容,先排查局域网内的网络问题(比如server2的防火墙规则、服务是否正常启动)。

5. 查看Apache错误日志定位具体问题

查看错误日志的详细信息,确认404是来自server1的规则不匹配,还是server2的路径无内容:

tail -f ${APACHE_LOG_DIR}/error.log

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 01:02:40