1.建立artisan命令,不瞭解可以參考我的上一篇文章使用 swoole 加速 Laravel5.6 Restful API 介面
2.新增/bootstrap/swoole.php檔案作為框架的啟動檔案
<?php
require_once __DIR__.'/../vendor/autoload.php';
try {
(new Dotenv\Dotenv(dirname(__DIR__)))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
$app = new Laravel\Lumen\Application(
dirname(__DIR__)
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
3.編寫操作類SwooleServerHandle
<?php
/**
* Created by PhpStorm.
* User: kite
*/
namespace App\Handles;
class SwooleServerHandle
{
//header Server 全域性變數的對映關係
private static $headerServerMapping = [
'x-real-ip' => 'REMOTE_ADDR',
'x-real-port' => 'REMOTE_PORT',
'server-protocol' => 'SERVER_PROTOCOL',
'server-name' => 'SERVER_NAME',
'server-addr' => 'SERVER_ADDR',
'server-port' => 'SERVER_PORT',
'scheme' => 'REQUEST_SCHEME',
];
public function onWorkerStart($serv,$worker_id)
{
require_once __DIR__.'/../../bootstrap/swoole.php;
}
public function onRequest($request,$response)
{
//server資訊
$_SERVER =[];
if (isset($request->server)) {
foreach ($request->server as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}
//header頭資訊
if (isset($request->header)) {
foreach ($request->header as $key => $value) {
if (isset(self::$headerServerMapping[$key])) {
$_SERVER[self::$headerServerMapping[$key]] = $value;
} else {
$key = str_replace('-', '_', $key);
$_SERVER[strtoupper('http_' . $key)] = $value;
}
}
}
//是否開啟https
if (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
//request uri
if (strpos($_SERVER['REQUEST_URI'], '?') === false && isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0
) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
//全域性的
if (!isset($_SERVER['argv'])) {
$_SERVER['argv'] = isset($GLOBALS['argv']) ? $GLOBALS['argv'] : [];
$_SERVER['argc'] = isset($GLOBALS['argc']) ? $GLOBALS['argc'] : 0;
}
//get資訊
$_GET = [];
if (isset($request->get)) {
foreach ($request->get as $k => $v) {
$_GET[$k] = $v;
}
}
//post資訊
$_POST = [];
if (isset($request->post)) {
foreach ($request->post as $k => $v) {
$_POST[$k] = $v;
}
}
//檔案請求
$_FILES =[];
if (isset($request->files)) {
foreach ($request->files as $k => $v) {
$_FILES[$k] = $v;
}
}
//cookie
$_COOKIE=[];
if (isset($request->cookie)) {
foreach ($request->cookie as $k => $v) {
$_COOKIE[$k] = $v;
}
}
ob_start();//啟用快取區
app()->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../../routes/web.php';
});
app()->run();
$res = ob_get_contents();//獲取快取區的內容
ob_end_clean();//清除快取區
//輸出快取區域的內容
$response->end($res);
}
}
4.使用php artisan swoole start即可開啟服務,在nginx配置代理.配置參考如下:
server {
listen 80;
server_name lumen.vip;
access_log /var/log/nginx/lumen.access.log main;
error_log /var/log/nginx/lumen.error.log error;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
proxy_pass http://127.0.0.1:9502;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結