Laravel 5 和 Cloudflare SSL

标签 laravel ssl nginx laravel-5.1 cloudflare

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

http://*example.com/*

为了确保它能正常工作,我向 public_html 添加了简单的 index.php 并且它能正常工作。我的意思是,当我进入该网站时,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

如有任何帮助,我们将不胜感激。

最佳答案

解决方案

强制所有路由到 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);
    }
}

关于Laravel 5 和 Cloudflare SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36785363/

相关文章:

sql - 选择多对多关系的最新记录

php - 如何在 helper 类中使用 laravel Eloquent 模型

ssl - Heroku 错误 : Websockets and ssl-endpoint features misconfiguration

apache - EC2 中的 http 到 https AWS 中的 apache2

ssl - Nginx 服务器 block 排序

javascript - 以 Angular 从 API JSON 中排序的项目日期

php - 如何使用 GuzzleHttp/cURL 指定公共(public)证书

ssl - heartbleed - 撤销或重新生成 SSL 证书?

nginx 仅将基本 '/' url 传递给gunicorn

ruby-on-rails - rails : HOST Header Attack vulnerability