thinkphp6 分布式 api环境集成 token验证 + redis + mysql + redis
2022-11-24 11:10:29
174
{{single.collect_count}}

thinkphp6 分布式 api环境集成 token验证 +  redis + mysql + redis

目录

        安装thinkPhp6 

集成JWT-AUTH(token 相关)

用phpstudy搭建网站服务

nginx 实现分布式负载均衡

thinkphp6配置允许跨域


  • 基础环境搭建 

下载安装 phpstudy 链接 : https://www.xp.cn

  • 安装redis

  • 安装php redis 支持

安装thinkPhp6 

集成JWT-AUTH 

用phpstudy搭建网站服务

设置伪静态:

location / {indexindex.html index.htm index.php;if (!-e $request_filename) {rewrite^(.*)$/index.php?s=/$1last;break;}#autoindexon;}

浏览器访问对应地址 http://localhost:8080/

如果一起顺利可以得到如下效果:

1.首先刪除app目录下所有其他文件2.安装多应用模式扩展: composer require topthink/think-multi-app3.创建三大应用index(前端),admin(后台),common(公共应用)命令:php think build app

nginx 实现分布式负载均衡

在8088、8089两个端口开两个服务,模拟两台服务器

然后nginx配置:

upstream app_http {server localhost:8088 weight=1;server localhost:8089 weight=1;}server {listen 80; location / { if (!-e $request_filename){proxy_pass http://app_http; } }}

thinkphp6配置允许跨域

//AllowCrossDomain<?phpnamespace app\middleware;class AllowCrossDomain{/** * 设置跨域 * @param $request * @param \Closure $next * @return mixed|void */public function handle($request, \Closure $next){header('Access-Control-Allow-Origin: *');header('Access-Control-Max-Age: 1800');header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, Token');if (strtoupper($request->method()) == "OPTIONS") {return Response::create()->send();}return $next($request);}}
//middleware.php<?php// 全局中间件定义文件return [// 全局请求缓存// \think\middleware\CheckRequestCache::class,// 多语言加载// \think\middleware\LoadLangPack::class,// Session初始化// \think\middleware\SessionInit::class\app\middleware\AllowCrossDomain::class];

 

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}