2016-04-22 94 views
2

那么,今天我第一次决定在我的web项目上使用ssl。首先,我在cloudflare.com中配置了我的域名。然后,我打开了cloudflare中的页面规则,自动将网站重定向到https。Laravel 5和Cloudflare SSL

http://*example.com/* 

为了确保它的作品,我添加了简单index.php到的public_html和它的工作。我的意思是,当我进入网站时,https被激活,并且该网站向我展示了着名的词语:“Hello World”

之后,我将我的Laravel项目上传到Web服务器并配置了虚拟主机。我使用nginx服务器。

server { 
     listen 80; 

     root /home/user/www/example.com/public_html/public; 
     index index.php index.html index.htm; 

     server_name example.com 

     location/{ 
       try_files $uri $uri/ =404; 
     } 

     error_page 404 /404.html; 
     error_page 500 502 503 504 /50x.html; 

     location = /50x.html { 
       root /usr/share/nginx/html; 
     } 

     location ~ \.php$ { 
       try_files $uri =404; 
       fastcgi_split_path_info ^(.+\.php)(/.+)$; 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$ 
       include fastcgi_params; 
     } 
} 

好吧,当我进入到与https网站,Laravel路线不工作。另外,我的css和js文件不被服务器读取。

然后我关掉了cloudflare的页面规则,网站使用http效果很好。

我试着用listen 443配置服务器,使用ssl on命令,但没有任何结果。

此外,我发现这个链接关于cloudflare与laravel 5.但不知道,这就是我想要的。

Laravel Cloudflare

任何帮助,将不胜感激。

回答

2

解决方案

迫使所有路由https我决定做一个中间件,并在所有路由都使用它。

namespace App\Http\Middleware; 

use Closure; 

class HttpsProtocol 
{ 
    public function handle($request, Closure $next) 
    { 
     $request->setTrustedProxies([ $request->getClientIp() ]); 
     if (!$request->secure()) { 
      return redirect()->secure($request->getRequestUri()); 
     } 

     return $next($request); 
    } 
}