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

Apache反向代理Node.js后端失效 求助排查问题

Apache反向代理Node.js后端失效问题

问题现象

Ubuntu环境下Apache服务器运行正常,已有多个VirtualHost可正常工作,但配置api.rpgrat.org反向代理至本地8000端口的Node.js服务时,访问该域名仍显示Apache文件系统,代理配置未生效。期望访问api.rpgrat.org时显示Node.js服务输出的API Backend内容。

当前Apache配置(000-defaults.conf)

ServerAdmin administrator@localhost
DocumentRoot /web/

<VirtualHost *:80>
    ServerName www.rpgrat.org
    ServerAlias rpgrat.org https://rpgrat.org
    DocumentRoot /web/static

    ErrorLog /web/logs/main_errors.log
    CustomLog /web/logs/main_other.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName play.rpgrat.org
    DocumentRoot /web/node

    ErrorLog /web/logs/node_errors.log
    CustomLog /web/logs/node_other.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName chess.rpgrat.org
        RewriteEngine on
    Redirect / https://rpgrat.org/chess
</VirtualHost>

# 存在问题的配置
<VirtualHost *:80>
    ServerName api.rpgrat.org
    DocumentRoot /web/public

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted
    </Proxy>
    proxyPass / http://localhost:8000/
    proxyPassReverse / http://localhost:8000/

    Header set Access-Control-Allow-Origin "*"
    ErrorLog /web/logs/public_errors.log
    CustomLog /web/logs/public_other.log combined
</VirtualHost>

Node.js后端服务状态

服务部署在localhost:8000,通过curl localhost:8000可正常返回API Backend,服务代码如下:

const http = require("http");
const host = 'localhost';
const port = 8000;

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end("API Backend\n");
};

const server = http.createServer(requestListener);
server.listen(port, host, () => {
    console.log(`API Server is running on http://${host}:${port}`);
});

已执行的排查操作

  • 启用Apache必要模块:
    sudo a2enmod proxy
    sudo a2enmod proxy_http
    sudo a2enmod proxy_balancer
    sudo a2enmod lbmethod_byrequests
    
  • 每次修改配置后均重启Apache服务
  • UFW已开放端口:80/tcp、443、8000
  • 本地测试:localhost:8000可正常访问Node服务,但api.localhost仍跳转至文件系统

错误日志内容

[Tue Jan 23 03:35:46.328697 2024] [authz_core:error] [pid 140060:tid 140348785419840] [client 137.184.150.232:59380] AH01630: client denied by server configuration: /web/public/server-status
[Tue Jan 23 03:35:47.993133 2024] [authz_core:error] [pid 140059:tid 140347770394176] [client 134.122.89.242:34688] AH01630: client denied by server configuration: /web/public/server-status

解决步骤

  1. 调整VirtualHost匹配顺序
    Apache按配置文件顺序匹配VirtualHost,将api.rpgrat.org的配置移至文件最顶部,确保优先匹配该域名请求。

  2. 移除冗余的DocumentRoot配置
    反向代理的VirtualHost无需设置DocumentRoot,该配置会导致Apache优先读取本地文件系统,删除DocumentRoot /web/public行。

  3. 修正指令大小写
    Apache指令区分大小写,将proxyPass改为ProxyPass,proxyPassReverse改为ProxyPassReverse。

  4. 验证配置语法合法性
    执行sudo apache2ctl configtest检查配置是否存在语法错误,确保修改后的配置合法。

  5. 重启Apache服务
    执行sudo systemctl restart apache2,使修改后的配置生效。

  6. 验证DNS解析
    确认api.rpgrat.org已正确解析至服务器IP,本地测试可临时修改hosts文件验证。

修改后的api.rpgrat.orgVirtualHost配置示例:

<VirtualHost *:80>
    ServerName api.rpgrat.org

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/

    Header set Access-Control-Allow-Origin "*"
    ErrorLog /web/logs/public_errors.log
    CustomLog /web/logs/public_other.log combined
</VirtualHost>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 15:02:04