【问题标题】:Magento 1.6.1 - Delete from cart redirects to homepage (with Varnish running)Magento 1.6.1 - 从购物车中删除重定向到主页(运行 Varnish)
【发布时间】:2012-03-13 16:04:28
【问题描述】:

我在 Magento 1.6.1 上遇到了一个非常奇怪的问题。

我们有 3 个平台,DEV、TST、LIVE(当前活跃的运行网站)。

我们也在现场运行 Varnish。

问题在于,在 LIVE 网站上,当您从购物车中删除某些东西(使用删除按钮)时,您总是会被重定向到主页。如果您通过将数量设置为 0 来删除项目,那么就可以了。

DEV 或 TST 上的相同代码库不会出现此问题。

这可能是清漆以某种方式干扰吗?有什么建议么?

【问题讨论】:

  • 我刚刚在一个网站上遇到了这个问题,因为 MW_Ajaxcart 扩展:你安装了这个扩展吗?

标签: magento e-commerce varnish


【解决方案1】:

您是否在开发/临时站点上运行 Varnish?听起来不像你。

我猜你的 Magento Varnish 模块有一个观察者来检测购物车是否有任何内容(即查看你是否真的有一个独特的会话),但是当你清空你的购物车时,这个观察者然后将 nocache 标头触发到 Varnish 并且 Varnish 返回一个新的(缓存的、非 cookie 的)响应并将您弹回主页。

听起来你的 Varnish 模块是原因。

删除 Varnish 及其关联的 Magento 模块,然后重新测试。似乎是一种相当简单的排除方法。

【讨论】:

  • 这就是我们得出的结论。看起来这可能与 Varnish 运行 Web 服务器的端口和 Magento 路由器中的内部 URL 检查有关,认为端口 8080 地址不是本地的。
  • 这就是问题所在。在端口 80 上运行 Nginx/Apache 会将重定向 URL 作为内部传递。然后使用 iptables 将传入流量传递给 Varnish。 erikeng.se/post/magento-behind-varnish.html
【解决方案2】:

在配置本地环境以在 Nginx 前使用 Varnish 时,让我印象深刻的一件事是应用程序重定向开始表现得很奇怪。从产品视图添加产品进行比较会将我重定向到基本 URL,而不是返回到产品视图(引荐来源网址)。所以我开始深入研究核心,看看到底发生了什么。

app/code/core/Mage/Core/Controller/Varien/Action.php:773

_getRefererUrl 方法在请求 URI 或请求标头中查找任何引用 URL。由于某种原因,$refererUrl 没有作为_isUrlInternal 传递,这使得它回退到基本 URL。

/**
 * Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param)
 *
 * @return string
 */
protected function _getRefererUrl()
{
    $refererUrl = $this->getRequest()->getServer('HTTP_REFERER');
    if ($url = $this->getRequest()->getParam(self::PARAM_NAME_REFERER_URL)) {
        $refererUrl = $url;
    }
    if ($url = $this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
        $refererUrl = Mage::helper('core')->urlDecode($url);
    }
    if ($url = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
        $refererUrl = Mage::helper('core')->urlDecode($url);
    }

    $refererUrl = Mage::helper('core')->escapeUrl($refererUrl);

    if (!$this->_isUrlInternal($refererUrl)) {
        $refererUrl = Mage::app()->getStore()->getBaseUrl();
    }
    return $refererUrl;
}

app/code/core/Mage/Core/Controller/Varien/Action.php:799

Magento 在这里寻找(基准 URL)字符串 http://domain.com/ 在(引用 URL)http://domain.com:8080/any/url.html 中的位置,由于 Nginx 正在侦听的端口号(在 Varnish 后面),因此永远找不到该位置。

/**
 * Check url to be used as internal
 *
 * @param   string $url
 * @return  bool
 */
protected function _isUrlInternal($url)
{
    if (strpos($url, 'http') !== false) {
        /**
         * Url must start from base secure or base unsecure url
         */
        if ((strpos($url, Mage::app()->getStore()->getBaseUrl()) === 0)
            || (strpos($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true)) === 0)
        ) {
            return true;
        }
    }
    return false;
}

app/code/core/Mage/Core/Helper/Url.php:37

在 Magento 中构建当前 URL 时(出于某种奇怪的原因)添加了该端口号。

/**
 * Retrieve current url
 *
 * @return string
 */
public function getCurrentUrl()
{
    $request = Mage::app()->getRequest();
    $port = $request->getServer('SERVER_PORT');
    if ($port) {
        $defaultPorts = array(
            Mage_Core_Controller_Request_Http::DEFAULT_HTTP_PORT,
            Mage_Core_Controller_Request_Http::DEFAULT_HTTPS_PORT
        );
        $port = (in_array($port, $defaultPorts)) ? '' : ':' . $port;
    }
    $url = $request->getScheme() . '://' . $request->getHttpHost() . $port . $request->getServer('REQUEST_URI');
    return $url;
}

让重定向再次起作用

在不改变应用程序的情况下,您的网络服务器 (Nginx) 需要监听端口 80(或 443),同时仍将 Varnish 保持在其前面。这可以通过使用 iptables 来完成。

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多