madkom / nginx-configurator
1.0.0-RC1
2016-06-10 11:19 UTC
Requires
- ferno/loco: @dev
- madkom/collection: ^1.0
- madkom/uri: ^1.0
Requires (Dev)
- phpspec/phpspec: ^2.5
- phpunit/phpunit: ~4
This package is not auto-updated.
Last update: 2024-09-23 13:01:07 UTC
README
PHP 库用于 NGINX 配置解析/生成器
特性
此库可以解析和生成 NGINX 配置文件。在不久的将来将提供用于 NGINX 配置的 CLI 命令。
安装
使用 Composer 安装
composer require madkom/nginx-configurator
要求
此库需要 PHP 版本 ~7
。
使用
解析配置字符串
use Madkom\NginxConfigurator\Builder; use Madkom\NginxConfigurator\Config\Server; use Madkom\NginxConfigurator\Parser; require 'vendor/autoload.php'; $config = <<<CONFIG server { listen 8080; root /data/www/web; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/www; } # pass the PHP scripts to FastCGI server listening on the php-fpm socket location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } CONFIG; $parser = new Parser(); $defaultConfig = $parser->parse($config); /** @var Server $defaultServers[] */ $defaultServers = $defaultConfig->search(function (Node $node) { return $node instanceof Server; }); $builder = new Builder(); if (count($defaultServers) > 0) { /** @var Server $defaultServer */ foreach ($defaultServers as $defaultServer) { $builder->append($defaultServer); } }
生成配置字符串
use Madkom\NginxConfigurator\Factory; use Madkom\NginxConfigurator\Builder; use Madkom\NginxConfigurator\Config\Location; use Madkom\NginxConfigurator\Node\Directive; use Madkom\NginxConfigurator\Node\Literal; use Madkom\NginxConfigurator\Node\Param; require __DIR__ . '/../vendor/autoload.php'; $factory = new Factory() $builder = new Builder(); $server = $builder->append($factory->createServer(80)); $server->append(new Directive('error_log', [ new Param('/var/log/nginx/error.log'), new Param('debug'), ])); $server->append(new Location(new Param('/test'), null, [ new Directive('error_page', [new Param('401'), new Param('@unauthorized')]), new Directive('set', [new Param('$auth_user'), new Literal('none')]), new Directive('auth_request', [new Param('/auth')]), new Directive('proxy_pass', [new Param('http://test-service')]), ])); $server->append(new Location(new Param('/auth'), null, [ new Directive('proxy_pass', [new Param('http://auth-service:9999')]), new Directive('proxy_bind', [new Param('$server_addr')]), new Directive('proxy_redirect', [new Param('http://$host'), new Param('https://$host')]), new Directive('proxy_set_header', [new Param('Content-Length'), new Literal("")]), new Directive('proxy_pass_request_body', [new Param('off')]), ])); $server->append(new Location(new Param('@unauthorized'), null, [ new Directive('return', [new Param('302'), new Param('/login?backurl=$request_uri')]), ])); $server->append(new Location(new Param('/login'), null, [ new Directive('expires', [new Param('-1')]), new Directive('proxy_pass', [new Param('http://identity-provider-service')]), new Directive('proxy_bind', [new Param('$server_addr')]), new Directive('proxy_redirect', [new Param('http://$host'), new Param('https://$host')]), new Directive('proxy_set_header', [new Param('Content-Length'), new Literal("")]), new Directive('proxy_pass_request_body', [new Param('off')]), ])); print($builder->dump());
生成的配置输出
server {
listen 80;
listen [::]:80 default ipv6only=on;
error_log /var/log/nginx/error.log debug;
location /test {
error_page 401 @unauthorized;
set $auth_user "none";
auth_request /auth;
proxy_pass http://test-service;
}
location /auth {
proxy_pass http://auth-service:9999;
proxy_bind $server_addr;
proxy_redirect http://$host https://$host;
proxy_set_header Content-Length "";
proxy_pass_request_body off;
}
location @unauthorized {
return 302 /login?backurl=$request_uri;
}
location /login {
expires -1;
proxy_pass http://identity-provider-service;
proxy_bind $server_addr;
proxy_redirect http://$host https://$host;
proxy_set_header Content-Length "";
proxy_pass_request_body off;
}
}
还有读取和转储文件的方法
use Madkom\NginxConfigurator\Builder; use Madkom\NginxConfigurator\Config\Location; use Madkom\NginxConfigurator\Config\Server; use Madkom\NginxConfigurator\Node\Directive; use Madkom\NginxConfigurator\Node\Literal; use Madkom\NginxConfigurator\Parser; require __DIR__ . '/../vendor/autoload.php'; $parser = new Parser(); $builder = new Builder(); $configuration = $parser->parseFile('default.conf'); /** @var Server $servers[] */ $servers = $configuration->search(function (Node $node) { return $node instanceof Server; }); if (count($servers) > 0) { /** @var Server $server */ foreach ($servers as $server) { $builder->append($server); } } $builder->dumpFile('generated.conf');
待办事项
- 实现注释解析
- 实现从 CLI 进行配置操作的命令
许可
MIT 许可证 (MIT)
版权所有 (c) 2016 Madkom S.A.
特此授予任何人免费获得此软件及其相关文档文件(“软件”)副本的权利,以无限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向获得软件的人提供软件,前提是满足以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何类型的保证,无论是明示的还是隐含的,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任负责,无论该责任是因合同、侵权或其他原因而产生的,无论是与软件或软件的使用或其他操作有关。