Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 and Cloudflare SSL

Well, today the first time I decided to use ssl on my web project. Firstly, I configured my domain in cloudflare.com. Then I turned on page rules in cloudflare, to automatic redirect the website to https.

http://*example.com/*

To make sure that it works, i added simple index.php to the public_html and it worked. I mean, when I entered to the web site, https was active and the site displayed me the famous word: "Hello World"

After that I uploaded my Laravel project to the web server and configured virtual host. I use nginx server.

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;
        }
}

Well, when I enter to the website with https, Laravel routes don't work. Also, my css and js files weren't read by server.

Then I turned off page rules from cloudflare, site worked good with http.

I tried to configure the server with listen 443, used ssl on command, but wasn't any result.

Also, I found this link about cloudflare with laravel 5. But not sure, this is that, what I want.

Laravel Cloudflare

Any help, would appreciated.

like image 386
Rashad Avatar asked Dec 30 '25 08:12

Rashad


1 Answers

Solution

forcing all routes to https I decided to make a middleware and use it in all routes.

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);
    }
}
like image 66
Rashad Avatar answered Jan 01 '26 12:01

Rashad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!