[romanpitak/nginx-config-processor]一个Nginx配置文件生成器

2022-03-21 奥古斯宏
使用对象的方法用法为Nginx生成配置文件

这是一个生成和处理Nginx配置文件的扩展库,可以使用简单地类方法生成Nginx的配置文件.

安装扩展非常简单:

composer require romanpitak/nginx-config-processor

比如这样:

<?php
require_once __DIR__.'/vendor/autoload.php';

use RomanPitak\Nginx\Config\Directive;
use RomanPitak\Nginx\Config\Scope;

Scope::create()
    ->addDirective(Directive::create('server')
        ->setChildScope(Scope::create()
            ->addDirective(Directive::create('listen', 8080))
            ->addDirective(Directive::create('server_name', 'example.net'))
            ->addDirective(Directive::create('root', 'C:/www/example_net'))
            ->addDirective(Directive::create('location', '^~ /var/', Scope::create()
                    ->addDirective(Directive::create('deny', 'all'))
                )->setCommentText('Deny access for location /var/')
            )
        )
    )
    ->saveToFile('example.net');

最终生成的配置文件如下:

server {
    listen 8080;
    server_name example.net;
    root C:/www/example_net;
    location ^~ /var/ { # Deny access for location /var/
        deny all;
    }
}

同时还支持其他的方法,比如设置注释等,同时也支持解析现有的配置文件,简单好用.

评论
点击登录