Laravel集成使用swoole的内存托管代码 — swooletw/laravel-swoole
swoole框架可以减少每次访问都需要读取磁盘io的弊端大大的算短程序运行时间,laravel就支持swoole的托管。省去了再去学习新的框架的时间。话不多说,开整:
1.使用composer安装
composer require swooletw/laravel-swoole
2.在 config/app.php 的 providers 数组中加上
SwooleTW\Http\LaravelServiceProvider::class
3.将配置文件导出到 config 目录下
php artisan vendor:publish --provider="SwooleTW\Http\HttpServiceProvider"
4.swoole_http.php 里还提供配置 providers 数组,因为使用swoole作为http后,这些providers会被存到内存中,所以这里配置的是每次请求都想要重新注册和重新启动的providers。
'providers' => [ // App\Providers\AuthServiceProvider::class, ]
5.这个轮子完全使用artisan命令来操作。
php artisan swoole:http start|stop|restart|reload
到这里,代码配置方面基本完成了 需要修改配置文件 nginx文件
直接覆盖lnmp生成的配置文件为下面的内容(这是配置http服务的方法。)
server
{
server_name fengbao.zhixiu.vip;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.api.com/public;
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix "/";
}
proxy_set_header Host $host;
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;
# IF https
# proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$suffix;
}
}
下面的内容是添加网站为ssl证书访问的情况下 swoole 做出的处理做了处理。。所有的80 443 均重定向443端口
upstream swoole {
# 通过 IP:Port 连接
server 127.0.0.1:1215 weight=5 max_fails=3 fail_timeout=30s;
keepalive 16;
}
server {
listen 80;
server_name www.talksphp.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
server_name www.talksphp.com;
index index.html index.htm index.php;
root /home/wwwroot/www.api.com/public;
ssl_certificate /etc/cert_files/talksphp/3706353_fengbao.zhixiu.vip.pem;
ssl_certificate_key /etc/cert_files/talksphp/3706353_fengbao.zhixiu.vip.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#关闭目录浏览
autoindex off;
# Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix "/";
}
proxy_set_header Host $host;
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;
# IF https
# proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$suffix;
}
access_log /home/wwwlogs/www.api.com.log;
}
6.每次修改代码需要重启
php artisan swoole restart
7.可以通过配置修改swoole_http.php的参数max_request=1; 可以在每次修改的时候不需要每次都输入重启命令。(可当作调试模式用使用,上线记得改回原来数值 或者改成0)