diff --git a/docs/chinese.md b/docs/chinese.md deleted file mode 100644 index 573ee72e..00000000 --- a/docs/chinese.md +++ /dev/null @@ -1,129 +0,0 @@ -# Laravel-Swoole-Http - -这个拓展包提供了一个基于[Swoole](http://www.swoole.com/)的高性能HTTP Server,它能帮助你大幅度地提高网站的并发能力。 - -## 安装 - -使用composer引入拓展包: - -``` -$ composer require huang-yi/laravel-swoole-http -``` - -> 该拓展包依赖于Swoole,请务必确保你的机器安装上了Swoole拓展。快速安装命令:`pecl install swoole`,详情请参考[Swoole官方网站](https://wiki.swoole.com/wiki/page/6.html)。 - -## 注册服务 - -如果你正在使用Laravel框架开发应用,请在`config/app.php`的providers数组中添加服务提供器: - -```php -[ - 'providers' => [ - SwooleTW\Http\LaravelServiceProvider::class, - ], -] -``` - -如果你正在使用Lumen框架开发应用,添加下面这行代码到`bootstrap/app.php`文件: - -```php -$app->register(SwooleTW\Http\LumenServiceProvider::class); -``` - -## 配置信息 - -如果你想修改默认配置项,请运行以下命令,它生成配置文件`http.php`在`config/`文件夹下: - -``` -$ php artisan vendor:publish -``` - -`server.host`:swoole_http_server监听的IP地址。 - -`server.port`: swoole_http_server监听的端口。 - -`server.options`: `Swoole\Server`的配置项. 更多细节请阅读[官方文档](https://wiki.swoole.com/wiki/page/274.html). - -例如你想调整max_request参数: - -```php -[ - 'server' => [ - 'options' => [ - 'max_request' => 1000, - ], - ] -] -``` - -## 命令 - -> swoole_http_server只能运行在cli模式下,因此该拓展包也提供了方便的artisan命令来做管理。 - -启动swoole_http_server: - -``` -$ php artisan swoole:http start -``` - -停止swoole_http_server: - -``` -$ php artisan swoole:http stop -``` - -重启swoole_http_server: - -``` -$ php artisan swoole:http restart -``` - -重载swoole_http_server: - -``` -$ php artisan swoole:http reload -``` - -## 配置Nginx - -> swoole_http_server对Http协议的支持并不完整,建议仅作为应用服务器,并且在前端增加Nginx作为代理。 - -```nginx -server { - listen 80; - server_name your.domain.com; - root /path/to/laravel/public; - index index.php; - - location = /index.php { - # 确保public目录下没有名字为not_exists的文件 - 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; - - # 如果使用了https协议 - # proxy_set_header HTTPS "on"; - - proxy_pass http://127.0.0.1:1215$suffix; - } -} -``` - -### 注意事项 - -每次发布新代码需要重载或重启swoole_http_server,因为swoole_http_server启动时会提前加载应用框架,使其常驻内存,这也是swoole_http_server高性能的原因之一。 \ No newline at end of file diff --git a/docs/english.md b/docs/english.md deleted file mode 100644 index eb898e2d..00000000 --- a/docs/english.md +++ /dev/null @@ -1,130 +0,0 @@ -# Laravel-Swoole-Http - -This package provides a high performance HTTP server which based on [Swoole](http://www.swoole.com/). - -## Installation - -Require this package with composer by using the following command: - -```sh -$ composer require huang-yi/laravel-swoole-http -``` - -> This package is relied on Swoole. Please make sure your machine has been installed the Swoole extension. Using this command to install quickly: `pecl install swoole`. Visit the [official website](https://wiki.swoole.com/wiki/page/6.html) for more information. - -## Service Provider - -If you are using Laravel, add the service provider to the providers array in `config/app.php`: - -```php -[ - 'providers' => [ - SwooleTW\Http\LaravelServiceProvider::class, - ], -] -``` - -If you are using Lumen, add the following code to `bootstrap/app.php`: - -```php -$app->register(SwooleTW\Http\LumenServiceProvider::class); -``` - -## Configuration - -If you want to change the default configurations, please run the following command to generate a configuration file `http.php` in directory `config/`: - -``` -$ php artisan vendor:publish -``` - -`server.host`: The swoole_http_server host. - -`server.port`: The swoole_http_server port. - -`server.options`: The configurations for `Swoole\Server`. To get more information about swoole server, please read [the official documentation](https://wiki.swoole.com/wiki/page/274.html). - -For example, if you want to set the 'max_request': - -```php -[ - 'server' => [ - 'options' => [ - 'max_request' => 1000, - ], - ] -] -``` - -## Command - -> The swoole_http_server can only run in cli environment, and this package provides convenient artisan commands to manage it. - -Start the swoole_http_server: - -``` -$ php artisan swoole:http start -``` - -Stop the swoole_http_server: - -``` -$ php artisan swoole:http stop -``` - -Restart the swoole_http_server: - -``` -$ php artisan swoole:http restart -``` - -Reload the swoole_http_server: - -``` -$ php artisan swoole:http reload -``` - -## Nginx Configuration - -> The swoole_http_server support for Http is not complete. So, you should configure the domains via nginx proxy. - -```nginx -server { - listen 80; - server_name your.domain.com; - root /path/to/laravel/public; - index index.php; - - 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; - } -} -``` - -## Notice - -You should reload or restart the swoole_http_server after released your code. Because the Laravel program will be kept in memory after the swoole_http_server started. That's why the swoole_http_server has high performance. diff --git a/src/Commands/HttpServerCommand.php b/src/Commands/HttpServerCommand.php index 66f9ec8f..0bcb42a7 100644 --- a/src/Commands/HttpServerCommand.php +++ b/src/Commands/HttpServerCommand.php @@ -85,7 +85,7 @@ protected function start() $port = $this->configs['server']['port']; $this->info('Starting swoole http server...'); - $this->info("Swoole http server listend on http://{$host}:{$port}."); + $this->info("Swoole http server started: "); if ($this->isDaemon()) { $this->info('> (You can run this command to ensure the ' . 'swoole_http_server process is running: ps aux|grep "swoole")'); diff --git a/src/Server/Application.php b/src/Server/Application.php index ad154195..3ad22239 100644 --- a/src/Server/Application.php +++ b/src/Server/Application.php @@ -2,7 +2,7 @@ namespace SwooleTW\Http\Server; -use Illuminate\Contracts\Foundation\Application as ApplicationContract; +use Illuminate\Container\Container; use Illuminate\Contracts\Http\Kernel; use Illuminate\Http\Request; @@ -86,7 +86,7 @@ protected function loadApplication() */ public function getApplication() { - if (! $this->application instanceof ApplicationContract) { + if (!$this->application instanceof Container) { $this->application = $this->loadApplication(); } @@ -98,7 +98,7 @@ public function getApplication() */ public function getKernel() { - if (! $this->kernel instanceof Kernel) { + if (!$this->kernel instanceof Kernel) { $this->kernel = $this->getApplication()->make(Kernel::class); } @@ -175,7 +175,7 @@ protected function setFramework($framework) { $framework = strtolower($framework); - if (! in_array($framework, ['laravel', 'lumen'])) { + if (!in_array($framework, ['laravel', 'lumen'])) { throw new \Exception(sprintf('Not support framework "%s".', $this->framework)); } diff --git a/src/Server/Manager.php b/src/Server/Manager.php index 79886e30..9a7abc95 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -167,9 +167,15 @@ public function onRequest($swooleRequest, $swooleResponse) $illuminateRequest = Request::make($swooleRequest)->toIlluminate(); $illuminateResponse = $this->getApplication()->run($illuminateRequest); - Response::make($illuminateResponse, $swooleResponse)->send(); + $response = Response::make($illuminateResponse, $swooleResponse); + // To prevent 'connection[...] is closed' error. + if (!$this->server->exist($response->getSwooleResponse()->fd)) { + return; + } + $response->send(); // Unset request and response. + $response = null; $swooleRequest = null; $swooleResponse = null; $illuminateRequest = null;