Laravel6整合swoole

tech2023-02-06  97

Laravel6整合swoole

项目中创建server文件夹(与app等文件夹同级)并创建文件http_server.php

<?php class http_server { private $http = null; public function __construct() { $this->http = new Swoole\Http\Server('0.0.0.0', 9501); $this->http->set([ 'worker_num' => 8, //进程数 'max_request' => 5000,// 最大连接数 'document_root' => '/wwwroot/Laravel/laravel6/public', //项目文件 'enable_static_handler' => true, ]); $this->http->on('start', [$this, 'onStart']); $this->http->on('WorkerStart', [$this, 'onWorkerStart']); $this->http->on('request', [$this, 'onRequest']); $this->http->start(); } // 启动提示 public function onStart($server) { echo 'Swoole服务启动成功...'; PHP_EOL; } // 回调加载框架 public function onWorkerStart($serv, $worker_id) { //加载index文件的内容 require __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../bootstrap/app.php'; } // 请求响应 public function onRequest($request, $response) { //server信息 if (isset($request->server)) { foreach ($request->server as $k => $v) { $_SERVER[strtoupper($k)] = $v; } } //header头信息 if (isset($request->header)) { foreach ($request->header as $k => $v) { $_SERVER[strtoupper($k)] = $v; } } //get请求 if (isset($request->get)) { foreach ($request->get as $k => $v) { $_GET[$k] = $v; } } //post请求 if (isset($request->post)) { foreach ($request->post as $k => $v) { $_POST[$k] = $v; } } //文件请求 if (isset($request->files)) { foreach ($request->files as $k => $v) { $_FILES[$k] = $v; } } //cookies请求 if (isset($request->cookie)) { foreach ($request->cookie as $k => $v) { $_COOKIE[$k] = $v; } } ob_start();//启用缓存区 //加载laravel请求核心模块 $kernel = app()->make(Illuminate\Contracts\Http\Kernel::class); $laravelResponse = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $laravelResponse->send(); $kernel->terminate($request, $laravelResponse); $res = ob_get_contents();//获取缓存区的内容 ob_end_clean();//清除缓存区 //输出缓存区域的内容 $response->end($res); } } new http_server();

部署laravel项目(Nginx配置,完成需重启nginx)

server { listen 80; server_name www.la.wang.com; root /wwwroot/Laravel/laravel6/public; location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?$query_string; #这一句是laravel部署必须的将index.php隐藏掉 } location ~ \.php$ { root /wwwroot/Laravel/laravel6/public; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } #location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { # root $root_path; #} #location ~ /\.ht { # deny all; #} }

项目测试访问

最新回复(0)