如何在 apache 配置条件中使用环境变量?

如何在 apache 配置条件中使用环境变量?

在我的 apache 配置文件中,我尝试在端口 443 上设置虚拟主机,如果我的环境变量不存在dev

我有一个通过 docker-compose 运行的 docker 容器,它指定了环境变量。

为了检查环境变量是否设置正确,我远程进入了我的容器:

root@4f212c1c7e23:/var/www# echo ${APP_ENV}
prod

下面是映射到容器的卷的一部分的 apache 配置文件

000-default.conf

<VirtualHost *:80>

  ServerName ${DOMAIN}
  ServerAdmin ${ADMIN_EMAIL}
  DocumentRoot /var/www/public

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

</VirtualHost>

<IfModule "%{APP_ENV} != 'dev'">
  <IfModule mod_ssl.c>
    <VirtualHost *:443>

      ServerName ${DOMAIN}
      ServerAdmin ${ADMIN_EMAIL}
      DocumentRoot /var/www/public
            
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
        
      SSLCertificateFile /var/imported/ssl/main.crt
      SSLCertificateKeyFile /var/imported/ssl/privkey.pem
      SSLEngine on

    </VirtualHost>
  </IfModule>
</IfModule>

但是在生产中启动容器时,尝试访问端口 443 时浏览器中的结果是“ERR_SSL_PROTOCOL_ERROR”。

我的错误日志仅显示:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.26.0.2. Set the 'ServerName' directive globally to suppress this message

类似地,表达式"%{APP_ENV} == 'prod'"给出相同的结果

当删除 apache 配置中的外部时IfModule,连接被接受并且一切都按预期工作。

所以我想知道如何让外部IfModule条件按预期运行?

如果我可以提供更多信息,请告诉我

相关内容