我的整个应用程序应该通过HTTPS访问,vhost配置正确,当我使用HTTPS前缀调用任何应用程序页面时,它都会正确响应。
然而,当我试图通过我的应用程序中生成的链接访问一个页面。它总是指向一个常规的HTTP链接。
我首先尝试使用。htaccess文件将所有流量重定向到HTTPS。我花了几个小时尝试了我能在网上找到的每一种重定向方法,但由于某种原因,我总是以无限重定向循环告终。
现在我正试图以一种不同的方式来解决这个问题:直接在HTTPS中生成我的链接。我所有的链接都是使用twig-bridge提供的URL()
函数生成的。
使用Symfony
配置非常容易但是我不知道如何用Silex来做。是否有一种简单的方法来改变Silex的路由方案,以强制每个链接生成一个HTTPS前缀?
将requireHttps()
添加到路由生成器中,将会奏效。
的例子:
$app->get('/hello-world', function($app) {
return 'Hello world';
})
->bind('hello') // Set route name (optional of course)
->requireHttps(); // This would force your url with https://
您也可以查看API以获取更多信息。你可以在那里找到你需要的一切。
设置环境变量HTTPS
为on
Nginx config inside location: fastcgi_param HTTPS on;
Apache的。htaccess或vhost config: SetEnv HTTPS on
请注意,官方文档中默认的nginx配置将此变量设置为off
,这可能会被复制到https站点,这是错误的:
http://silex.sensiolabs.org/doc/web_servers.html
Ctrl+F - fastcgi_param HTTPS off;
如果您使用控制器提供程序,您可以使用Controller
类中的requireHttps()
方法来要求整个路由范围的HTTPS:
namespace Acme;
use Silex'Application;
use Silex'Api'ControllerProviderInterface;
class HelloControllerProvider implements ControllerProviderInterface
{
public function connect(Application $app)
{
$controllers = $app['controllers_factory'];
$controllers->get('/', function (Application $app) {
return $app->redirect('/hello');
});
$controllers->get('/another-page', 'Acme'HelloControllerProvider::doSomething')->bind('another-page');
// here it goes
$controllers->requireHttps();
// or even make it more flexible with a config variable
// if ($app['config']['require_https']) (
// $controllers->requireHttps();
// }
return $controllers;
}
}
对于这种特殊情况,我解决了在app.php中声明基url的问题:
$app['customBaseUrl'] = 'https://mybaseurl';
然后用这个自定义baseUrl作为所有链接的前缀。这些链接不再由url()分支函数生成。
虽然不是最好的方法。如果您的服务器配置允许使用,我建议您使用Artamiel的答案。
另一个选择是在SecurityServiceProvider设置中使用access_control(如果你正在使用这个设置)来指定某些路由需要使用https'requires_channel' =>安全配置的access_control部分中的'https'