2016-04-22 82 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); 
    } 
}