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

在AWS通过Terraform部署Apache2时出现502 Bad Gateway错误求助

问题排查:Apache2迁移后Nginx转发出现502 Bad Gateway错误

我们将Apache2配置迁移至原使用Nginx的Terraform代码仓库(用于支持请求重定向),切换后运行apache2-foreground时,访问网站出现502 Bad Gateway错误,同时重定向、页面刷新功能异常。当前网站流量仍通过Nginx路由转发至Apache2服务器或后端服务器。

应用Dockerfile内容

FROM node:latest AS build
WORKDIR /build
COPY --chown=node:node . /build
COPY --chown=node:node ./webapp.env /build/.env
RUN npm install
RUN npm run build
USER node

FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl wget apache2 && rm -rf /var/lib/apt/lists/*
COPY ./000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ./ports.conf /etc/apache2/ports.conf
COPY ./apache2.conf /etc/apache2/apache2.conf
COPY --from=build /build/build/ /var/www/html/cadis
EXPOSE 8080
CMD [ "apache2-foreground" ]

Apache关键配置文件000-default.conf

<VirtualHost *:8080>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/cadis

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    <Directory /var/www/html/cadis>
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
    </Directory>

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

排查方向

  • 检查Nginx上游配置:确认Nginx是否正确指向Apache2的8080端口,排查容器网络连通性(如是否在同一Docker网络、Nginx能否解析Apache容器地址),避免端口映射或地址配置错误。
  • 验证Apache服务状态:进入Apache容器执行apache2ctl status查看服务状态,或执行tail /var/log/apache2/error.log读取错误日志,确认Apache是否正常监听8080端口,是否存在配置语法错误、文件权限问题。
  • 修正静态文件权限:确认/var/www/html/cadis目录及文件权限允许Apache读取,可执行chown -R www-data:www-data /var/www/html/cadis将权限归属Apache默认用户组。
  • 补充ServerName配置:在000-default.conf中取消注释并设置ServerName dev.cadis-ezi.com,避免Apache生成错误的重定向URL。
  • 启用必要Apache模块:前端SPA应用的路由刷新通常依赖rewrite模块,执行a2enmod rewrite启用模块后重启Apache服务。
  • 检查端口配置:确认ports.conf中存在Listen 8080配置,确保Apache监听正确端口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 18:32:17