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

Apache2反向代理配置:SPA子目录路由错误及子域名解决方案

SPA在Apache子路径路由失败的解决过程

我搭建了一个SPA,部署在服务器上的访问地址是http://my-server:3000。之后在同一台服务器安装了Apache2,创建了通过My-domain域名对外提供服务的静态站点。

原本计划让这个SPA在静态站点的/foo路径下运行,所以配置了如下Apache2虚拟主机:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName My-domain

        ProxyRequests Off
        ProxyHTMLEnable On
        ProxyHTMLExtended Off
        ProxyPreserveHost On

        ProxyPass /foo/ http://my-server:3000/

        <Location /foo/>
                ProxyHTMLURLMap / /foo/
                ProxyPassReverse http://my-server:3000/
                SetOutputFilter INFLATE;DEFLATE
        </Location>
        <Proxy *>
                Order allow,deny
                Allow from all
        </Proxy>

        LogLevel info

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

配置完成后,SPA的文件能正常加载,但启动时出现No match for URL in routes错误。推测是因为SPA默认以根目录运行,导致路由路径和实际访问的子路径不匹配。

最终我通过配置子域名foo.My-domain解决了问题,修改后的虚拟主机配置如下:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName foo.My-domain

        ProxyRequests Off
        ProxyHTMLEnable On
        ProxyHTMLExtended Off
        ProxyPreserveHost On

        ProxyPass / http://my-server:3000/

        <Location />
                ProxyPassReverse http://my-server:3000/
                SetOutputFilter INFLATE;DEFLATE
        </Location>
        <Proxy *>
                Order allow,deny
                Allow from all
        </Proxy>

        LogLevel info

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

此时SPA以根目录模式运行,路由错误消失,恢复正常使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 13:25:17