SSL, Nginx and Magento

SSL, Nginx and Magento 这三件东西对我都不陌生。但三件全排在一起,着实挑战了我一下,发生错误是 secure page redirect loop。

Magento secure page redirect loop
Magento secure page redirect loop

原因是 $_SERVER[] 里缺少 HTTPS directive,需要在 fastcgi_params 里添加一行

fastcgi_param HTTPS on;

以 Magento 1.4.0.1 为例深究一下—— 在 $_SERVER[‘HTTPS’] 缺失时 app/code/core/Mage/Core/Model/Store.php 的 isCurrentlySecure() 返回值 false,所以 Magento 不停地重定向到 secure url 而不知道当前 url 已经是 secure 了。


public function isCurrentlySecure()
{
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
return true;
}

if (Mage::isInstalled()) {
$secureBaseUrl = Mage::getStoreConfig('web/secure/base_route_url');
if (!$secureBaseUrl) {
return false;
}
$uri = Zend_Uri::factory($secureBaseUrl);
$isSecure = ($uri->getScheme() == 'https' )
&& isset($_SERVER['SERVER_PORT'])
&& ($uri->getPort() == $_SERVER['SERVER_PORT']);
return $isSecure;
} else {
$isSecure = isset($_SERVER['SERVER_PORT']) && (443 == $_SERVER['SERVER_PORT']);
return $isSecure;
}
}

我一直觉得用 php 探测当前 protocol 是否为 https 的算法比较“土”,曾经以为会有更好的探测办法,目前看来是没有。不光是没有,而且不可能有。就如有个小秘负责拆信,然后把有用的信纸交给 CEO 阅读,所以 CEO 不可能知道某封信是拿什么信封装的。

3 comments

  1. 我的magento在nginx下运行正常,现在在刚安装了verisign的30天免费ssl在测试,到目前,ssl证书 似乎已经正常安装了。但使用https访问如何内页都会跑回到主页的http页面。现在想问一苑主:后面需要怎么做才能让magento全部链接使用https?有没啥教程?

  2. 嗯,现在改了。可以用了。只是在checkout的时候出现https。芳草老大在国外闲逛啊!谢谢!!

Leave a comment

Your email address will not be published. Required fields are marked *