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

Docker环境下Magento 2应用无限重定向问题求助

Magento 2 Docker容器无限重定向问题解决方案

1. 修复外部反向代理的HTTPS头传递

在外部Apache的SSL虚拟主机(443端口)配置中,添加协议头传递配置,确保容器内的Magento能识别外部的HTTPS请求:

ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"

修改后的完整SSL配置片段:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName project.example.com

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

        ProxyPreserveHost On
        RequestHeader set X-Forwarded-Proto "https"
        RequestHeader set X-Forwarded-Port "443"
        ProxyPass / http://localhost:8000/
        ProxyPassReverse / http://localhost:8000/

        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/project.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/project.example.com/privkey.pem
</VirtualHost>
</IfModule>

2. 容器内Apache配置优化

  1. 启用必要的Apache模块:
a2enmod headers remoteip
  1. 在容器内的Apache虚拟主机配置中添加信任代理IP的规则(替换172.17.0.1为实际Docker网关IP):
RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 172.17.0.1

3. 调整Magento系统配置

进入容器内的Magento项目目录,执行以下CLI命令:

# 设置正确的HTTPS基础URL
bin/magento setup:store-config:set --base-url="https://project.example.com/"
bin/magento setup:store-config:set --base-url-secure="https://project.example.com/"

# 启用URL重写
bin/magento config:set web/seo/use_rewrites 1

# 配置Magento识别转发的HTTPS协议
bin/magento config:set web/secure/offloader_header X-Forwarded-Proto
bin/magento config:set web/unsecure/offloader_header X-Forwarded-Proto

# 强制前后端使用HTTPS
bin/magento config:set web/secure/use_in_frontend 1
bin/magento config:set web/secure/use_in_adminhtml 1

# 清除所有缓存
bin/magento cache:flush

4. 重启服务生效

  • 外部主机重启Apache:
systemctl restart apache2
  • 容器内重启Apache:
service apache2 restart

最后清除浏览器缓存与Cookie,重新访问站点验证问题是否解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 12:16:18