LaravelS基于Swoole实现高性能 HTTP 服务器

Posted 码农有券

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LaravelS基于Swoole实现高性能 HTTP 服务器相关的知识,希望对你有一定的参考价值。

LaravelS Swoole Laravel

安装配置 LaravelS 安装Laravel 5.5以上版本 composer中查看laravel框架

   
 composer search laravel //显示出laravel包

查看laravel包具体信息

    
composer show --all laravel/laravel // 可以显示出包名(name) 描述(descrip) 版本(versions)等

create-project 命令来安装 Laravel 应用:

   
 // 直接安装,默认安装的最新版 composer create-project --prefer-dist laravel/laravel (文件名) // 安装制定版本的 laravel composer create-project --prefer-dist laravel/laravel blog 5.6.*

  
 // 阿里云镜像 composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ // 阿里云镜像


安装 LaravelS

   
 composer require hhxsv5/laravel-s

安装完成后,运行如下 Artisan 命令相应脚本和配置文件发布到根目录下:
`php artisan laravels publish`

该命令会发布配置文件 laravels.php 到 config 目录下,以及脚本文件到 bin 目录下:

启动 LaravelS

完成上述操作后,就可以通过php bin/laravels start 命令启动 LaravelS 了:LaravelS基于Swoole实现高性能 HTTP 服务器这样,Swoole 服务就被启动起来,监听本地的 5200 端口,如果有请求发送到这个端口,它就可以进行处理。此外 php bin/laravels 还支持其它命令对 LaravelS 进行管理: 

基于 LaravelS 构建 HTTP 服务器 配置 nginx 我们知道在使用 Nginx 作为 Web 服务器的时候,前端资源文件,比如 CSS、JS、图片等静态资源都是通过 Nginx 进行处理的,比较高效,而 PHP 脚本请求这种动态资源都是转发到后端 PHP-FPM 进程进行处理,如果要基于 Swoole 实现高性能 HTTP 服务器,则这个 HTTP 服务器替代的也是 PHP-FPM 的职能,也就是说,我们将原本转发到 PHP-FPM 进程的请求转发给 Swoole 进行处理。在本例中,就是转发给 LaravelS 服务。伪静态中修改为:

 location / {  try_files $uri @laravels; }
配置文件 增加:
map $http_upgrade $connection_upgrade { default upgrade; '' close; }upstream swoole { # Connect IP:Port server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s; # Connect UnixSocket Stream file, tips: put the socket file in the /dev/shm directory to get better performance #server unix:/yourpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s; #server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s; #server 192.168.1.2:5200 backup; keepalive 16;}
server{
location =/ws { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout: Nginx will close the connection if the proxied server does not send data to Nginx in 60 seconds; At the same time, this close behavior is also affected by heartbeat setting of Swoole. # proxy_read_timeout 60s; proxy_http_version 1.1; 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_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass http://swoole;}location @laravels { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout 60s; proxy_http_version 1.1; proxy_set_header Connection ""; 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://swoole;}
}


配置 Laravel 应用
接下来,项目根目录下打开 .env,新增下面两条配置:

LARAVELS_LISTEN_IP=workspaceLARAVELS_DAEMONIZE=true


到项目目录下运行 php bin/laravels start 启动 LaravelS 后端服务,此时,该服务会在后端运行:

nginx配置列

map $http_upgrade $connection_upgrade { default upgrade; '' close;}upstream swoole { # Connect IP:Port server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s; # Connect UnixSocket Stream file, tips: put the socket file in the /dev/shm directory to get better performance #server unix:/yourpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s; #server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s; #server 192.168.1.2:5200 backup; keepalive 16;}server{ listen 80; listen 443 ssl http2; listen [::]:443 ssl http2; listen [::]:80; server_name blogs.chencong.fun; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/blogs.chencong.fun/public;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; ssl_certificate /www/server/panel/vhost/cert/blogs.chencong.fun/fullchain.pem; ssl_certificate_key /www/server/panel/vhost/cert/blogs.chencong.fun/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-72.conf; #PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/blogs.chencong.fun.conf; #REWRITE-END
# Response 404 directly when request the PHP file, to avoid exposing public/*.php #location ~* \.php$ { # return 404; #} # Http and WebSocket are concomitant, Nginx identifies them by "location" # !!! The location of WebSocket is "/ws" # javascript: var ws = new WebSocket("ws://laravels.com/ws"); location =/ws { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout: Nginx will close the connection if the proxied server does not send data to Nginx in 60 seconds; At the same time, this close behavior is also affected by heartbeat setting of Swoole. # proxy_read_timeout 60s; proxy_http_version 1.1; 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_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass http://swoole; } location @laravels { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout 60s; proxy_http_version 1.1; proxy_set_header Connection ""; 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://swoole; } #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; }
#一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log off; access_log /dev/null; }
location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log /dev/null; }
access_log /www/wwwlogs/blogs.chencong.fun.log; error_log /www/wwwlogs/blogs.chencong.fun.error.log;}


以上是关于LaravelS基于Swoole实现高性能 HTTP 服务器的主要内容,如果未能解决你的问题,请参考以下文章

在laravel5.8中集成swoole组件----用协程实现的服务端和客户端---静态文件如何部署

laravel下安装laravels以及websocket的使用

基于nginx+swoole+phalcon+atlas实现的高性能负载均衡集群系列之构建篇

GitHub 上都有哪些比较有趣的 PHP 项目?

基于 Swoole 搭建 WebSocket 服务详解

如何使用hhxsv5/laravel-s的异步任务队列