美文网首页
swoole加速laravel

swoole加速laravel

作者: 耍帅oldboy | 来源:发表于2022-11-14 16:40 被阅读0次

背景介绍

原因是php-fpm的工作模式是每次请求都要从硬盘加载一堆的php文件,消耗磁盘IO,速度慢,使用swoole可以一次请求把所有的文件都加载到内存,后面的请求都可以从内次加载php文件,加快laravel的处理速度,laravel一个请求从200ms降低到了20ms

安装swoole扩展不介绍,

1、安装laravel-swoole

composer require swooletw/laravel-swoole

3、在config/app.php的数组providers增加

SwooleTW\Http\LaravelServiceProvider::class

2、安装配置

修改端口9501

php artisan vendor:publish --tag=laravel-swoole

3、nginx配置

把url的请求转发到swoole服务地址127.0.0.1:9501

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server
    {
    listen 80;
        server_name www.xxx.com ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/www.xxx.com/public;

       location = /index.php {
            try_files /not_exists @swoole;
        }   

    location / {
            try_files $uri $uri/ @swoole;
        }

    location @swoole {
    set $suffix "";
        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://127.0.0.1:9503$suffix;
    }   
 }

4、启动服务器

php artisan swoole:http start

相关文章

网友评论

      本文标题:swoole加速laravel

      本文链接:https://www.haomeiwen.com/subject/rbvlxdtx.html