{"id":21861003,"url":"https://github.com/sasha-bo/nginx-conf-parser","last_synced_at":"2025-04-14T19:22:01.057Z","repository":{"id":198941038,"uuid":"701837680","full_name":"sasha-bo/nginx-conf-parser","owner":"sasha-bo","description":"Parsing nginx.conf files with/without following includes","archived":false,"fork":false,"pushed_at":"2024-02-03T09:30:14.000Z","size":39,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T07:41:38.202Z","etag":null,"topics":["nginx","nginx-conf","nginx-configuration","parser","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sasha-bo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-10-07T17:50:36.000Z","updated_at":"2024-09-08T00:50:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"8dcb511d-514d-4479-b243-071698b6a25e","html_url":"https://github.com/sasha-bo/nginx-conf-parser","commit_stats":null,"previous_names":["sasha-bo/nginx-con-parser"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-bo%2Fnginx-conf-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-bo%2Fnginx-conf-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-bo%2Fnginx-conf-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasha-bo%2Fnginx-conf-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sasha-bo","download_url":"https://codeload.github.com/sasha-bo/nginx-conf-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943415,"owners_count":21186958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nginx","nginx-conf","nginx-configuration","parser","php"],"created_at":"2024-11-28T03:09:25.551Z","updated_at":"2025-04-14T19:22:01.041Z","avatar_url":"https://github.com/sasha-bo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nginx-conf-parser\n\nA PHP library for parsing nginx.conf files. No dependencies.\n\n## Installation\n\n`composer require sashabo/nginx-conf-parser`\n\n## Usage\n\nTo parse nginx.conf from string use static method:\n\n`SashaBo\\NginxConfParser\\Parser::parseString(string $source): array`\n\nTo parse from file:\n\n`SashaBo\\NginxConfParser\\Parser::parseFile(string $path, bool $followIncludes = false): array`\n\nBoth return ```array\u003cSashaBo\\NginxConfParser\\Row\u003e```:\n\n```php\nclass Row\n{\n    public readonly string $name;\n    /** @var array\u003cstring\u003e */\n    public readonly array $values;\n    /** @var array\u003cself\u003e */\n    public readonly array $rows;\n    public readonly ?string $file;\n    /** @var non-negative-int */\n    public readonly int $position;\n    /** @var non-negative-int */\n    public readonly int $line;\n    /** @var non-negative-int */\n    public readonly int $linePosition;\n    /** @var positive-int */\n    public readonly int $length;\n}\n```\n\n`name` - the name of nginx.conf parameter.\n\n`values` - an array or words going after the name. For example:\n\n```\nname: 'server_name'\nvalues: ['my-domain.com', 'my-alias.com']\n```\n\n`rows` - an array of rows in {...}. \n\nSo, a row with name *'http'* contains rows with name *'server'*, and they -\nrows with name *'location'*.\n\n`position`, `line` and `linePosition` start from 0. For the *'server_name'* \nnginx parameter these three values describe the position of 's' symbol.\n\n`length` is counted from the first symbol of the name ('s' for \n*'server_name'*) to the ';' or '}' symbol including them. So you can use \n`position` and `length` to remove or replace the whole nginx parameter or \nthe block of parameters with substr_replace, and your nginx.conf will be \nstill valid.\n\n## Behavior\n\nThe parser doesn't interpret nginx.conf data, just parses. \nIt knows nothing about nginx parameters, just follows the syntax.\nIf your nginx.conf file contains wrong data, the parser won't throw \nexceptions while the structure of the file is correct.\n\nThe only command the parser understands is *include* if `$followIncludes`\nis `true`. In this case, parser will parse also included file (or few files \nif there is a pattern like *sites-enabled/\\*.conf*) and replace the \n*include* row with rows from the included file.\n\n## Composing nginx.conf\n\nThe package contains a tool for composing parsed (or generated) data back\nto nginx.conf file. To do this, use static method\n`SashaBo\\NginxConfParser\\Composer::compose(array $rows): string`\n\n## Searching parameters in nginx.conf\n\nSince 1.2.0 the package contains Finder class with these methods:\n\n```php\nclass Finder\n{\n    /**\n     * @param array\u003cRow\u003e $rows\n     * @return array\u003cRow\u003e\n     */\n    public static function findByName(array $rows, string $name): array\n\n    /**\n     * @param array\u003cRow\u003e $rows\n     * @return ?Row\n     */\n    public static function findOneByName(array $rows, string $name): ?Row\n\n    /**\n     * @param array\u003cRow\u003e    $rows\n     * @param array\u003cstring\u003e $path\n     * @return array\u003cRow\u003e\n     */\n    public static function findByPath(array $rows, array $path): array\n\n    /**\n     * @param array\u003cRow\u003e    $rows\n     * @param array\u003cstring\u003e $path\n     * @return ?Row\n     */\n    public static function findOneByPath(array $rows, array $path): ?Row\n}\n```\n\n## Using variables\n\nSince 1.2.0 the package contains VariableReplacer class with these methods:\n\n```php\nclass VariableReplacer\n{\n    public const MISSED_SET_EMPTY = 'set-empty';\n    public const MISSED_IGNORE = 'ignore';\n    public const MISSED_THROW_EXCEPTION = 'exception';\n\n    /**\n     * @param array\u003cRow\u003e            $rows\n     * @param array\u003cstring, string\u003e $variables\n     * @return array\u003cRow\u003e\n     */\n    public static function replace(array $rows, array $variables, string $missedMode = self::MISSED_IGNORE): array\n\n    public static function replaceRow(Row $row, array $variables, string $missedMode = self::MISSED_IGNORE): Row\n}\n```\n\nExample:\n\n```\nserver {\n    listen $server_port;\n    root $server_root;\n\n    location / {\n        try_files $uri @php;\n    }\n    location @php {\n        include fastcgi.conf;\n        fastcgi_pass localhost:9000;\n        fastcgi_param SCRIPT_FILENAME /app/app.php;\n    }\n}\n```\n\n```php\n$rows = VariableReplacer::replace(\n    Parser::parseFile('/etc/nginx/nginx.conf'),\n    [\n        'server_port' =\u003e 80,\n        'server_root' =\u003e '/app/public'\n    ]\n);\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsasha-bo%2Fnginx-conf-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsasha-bo%2Fnginx-conf-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsasha-bo%2Fnginx-conf-parser/lists"}