Laravel只允许访问https

版权声明:经验之谈,不知能否换包辣条,另,转载请注明出处。 https://blog.csdn.net/zhezhebie/article/details/86736419

Laravel中间件跳转到https:

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        URL::forceScheme('https');
    }
}

参考:
https://stackoverflow.com/questions/28402726/laravel-5-redirect-to-https

以上是程序方式,通过启动里面控制.这是如果你不能修改服务端的配置的情况下使用的.如果你能够修改nginx或者apache的配置文件,那就很简单了.

Apache:

RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

nginx:

  server {
       listen       443;
       server_name  blog.xxx.top;
       ssl                  on;
       ssl_certificate      /xx/xx/xxx.pem;
       ssl_certificate_key  /xx/xx/xxx.key;
       ssl_session_timeout  5m;
       ssl_protocols  SSLv2 SSLv3 TLSv1;
       ssl_ciphers  HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers   on;
 
      root   /www/myBlog/public;
      index  index.html index.htm index.php;
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
      location ~ \.php$ {
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
      }
  }
  server {
       listen 80;
       server_name  blog.xxx.top;
       #告诉浏览器有效期内只准用 https 访问
       add_header Strict-Transport-Security max-age=15768000;
       #永久重定向到 https 站点
       return 301 https://$server_name$request_uri;
   }

猜你喜欢

转载自blog.csdn.net/zhezhebie/article/details/86736419