【问题标题】:Laravel 5 and Cloudflare SSLLaravel 5 和 Cloudflare SSL
【发布时间】:2016-08-15 14:02:39
【问题描述】:

好吧,今天我第一次决定在我的网络项目中使用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 命令,但没有任何结果。

另外,我在 laravel 5 中找到了关于 cloudflare 的链接。但不确定,这就是我想要的。

Laravel Cloudflare

任何帮助,将不胜感激。

【问题讨论】:

    标签: laravel ssl nginx laravel-5.1 cloudflare


    【解决方案1】:

    解决方案

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

    【讨论】:

      猜你喜欢
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2017-09-24
      • 2014-10-09
      • 2019-04-08
      • 2019-05-30
      相关资源
      最近更新 更多