PHPackages                             sashabo/nginx-conf-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sashabo/nginx-conf-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sashabo/nginx-conf-parser
=========================

Parsing nginx.conf files and following includes

1.2.1(2y ago)23.8k↑450%MITPHPPHP &gt;=8.1

Since Jan 18Pushed 2y ago1 watchersCompare

[ Source](https://github.com/sasha-bo/nginx-conf-parser)[ Packagist](https://packagist.org/packages/sashabo/nginx-conf-parser)[ RSS](/packages/sashabo-nginx-conf-parser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (6)Used By (0)

nginx-conf-parser
=================

[](#nginx-conf-parser)

A PHP library for parsing nginx.conf files. No dependencies.

Installation
------------

[](#installation)

`composer require sashabo/nginx-conf-parser`

Usage
-----

[](#usage)

To parse nginx.conf from string use static method:

`SashaBo\NginxConfParser\Parser::parseString(string $source): array`

To parse from file:

`SashaBo\NginxConfParser\Parser::parseFile(string $path, bool $followIncludes = false): array`

Both return `array`:

```
class Row
{
    public readonly string $name;
    /** @var array */
    public readonly array $values;
    /** @var array */
    public readonly array $rows;
    public readonly ?string $file;
    /** @var non-negative-int */
    public readonly int $position;
    /** @var non-negative-int */
    public readonly int $line;
    /** @var non-negative-int */
    public readonly int $linePosition;
    /** @var positive-int */
    public readonly int $length;
}
```

`name` - the name of nginx.conf parameter.

`values` - an array or words going after the name. For example:

```
name: 'server_name'
values: ['my-domain.com', 'my-alias.com']

```

`rows` - an array of rows in {...}.

So, a row with name *'http'* contains rows with name *'server'*, and they - rows with name *'location'*.

`position`, `line` and `linePosition` start from 0. For the *'server\_name'*nginx parameter these three values describe the position of 's' symbol.

`length` is counted from the first symbol of the name ('s' for *'server\_name'*) to the ';' or '}' symbol including them. So you can use `position` and `length` to remove or replace the whole nginx parameter or the block of parameters with substr\_replace, and your nginx.conf will be still valid.

Behavior
--------

[](#behavior)

The parser doesn't interpret nginx.conf data, just parses. It knows nothing about nginx parameters, just follows the syntax. If your nginx.conf file contains wrong data, the parser won't throw exceptions while the structure of the file is correct.

The only command the parser understands is *include* if `$followIncludes`is `true`. In this case, parser will parse also included file (or few files if there is a pattern like *sites-enabled/\*.conf*) and replace the *include* row with rows from the included file.

Composing nginx.conf
--------------------

[](#composing-nginxconf)

The package contains a tool for composing parsed (or generated) data back to nginx.conf file. To do this, use static method `SashaBo\NginxConfParser\Composer::compose(array $rows): string`

Searching parameters in nginx.conf
----------------------------------

[](#searching-parameters-in-nginxconf)

Since 1.2.0 the package contains Finder class with these methods:

```
class Finder
{
    /**
     * @param array $rows
     * @return array
     */
    public static function findByName(array $rows, string $name): array

    /**
     * @param array $rows
     * @return ?Row
     */
    public static function findOneByName(array $rows, string $name): ?Row

    /**
     * @param array    $rows
     * @param array $path
     * @return array
     */
    public static function findByPath(array $rows, array $path): array

    /**
     * @param array    $rows
     * @param array $path
     * @return ?Row
     */
    public static function findOneByPath(array $rows, array $path): ?Row
}
```

Using variables
---------------

[](#using-variables)

Since 1.2.0 the package contains VariableReplacer class with these methods:

```
class VariableReplacer
{
    public const MISSED_SET_EMPTY = 'set-empty';
    public const MISSED_IGNORE = 'ignore';
    public const MISSED_THROW_EXCEPTION = 'exception';

    /**
     * @param array            $rows
     * @param array $variables
     * @return array
     */
    public static function replace(array $rows, array $variables, string $missedMode = self::MISSED_IGNORE): array

    public static function replaceRow(Row $row, array $variables, string $missedMode = self::MISSED_IGNORE): Row
}
```

Example:

```
server {
    listen $server_port;
    root $server_root;

    location / {
        try_files $uri @php;
    }
    location @php {
        include fastcgi.conf;
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME /app/app.php;
    }
}

```

```
$rows = VariableReplacer::replace(
    Parser::parseFile('/etc/nginx/nginx.conf'),
    [
        'server_port' => 80,
        'server_root' => '/app/public'
    ]
);
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

5

Last Release

836d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5369c71152f0feb0c57d12044c87c7f8be9c4640586b042629a6c50638d4e2e8?d=identicon)[sashabo](/maintainers/sashabo)

---

Top Contributors

[![xbndrv](https://avatars.githubusercontent.com/u/151878218?v=4)](https://github.com/xbndrv "xbndrv (15 commits)")[![sasha-bo](https://avatars.githubusercontent.com/u/146867796?v=4)](https://github.com/sasha-bo "sasha-bo (1 commits)")

---

Tags

nginxnginx-confnginx-configurationparserphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sashabo-nginx-conf-parser/health.svg)

```
[![Health](https://phpackages.com/badges/sashabo-nginx-conf-parser/health.svg)](https://phpackages.com/packages/sashabo-nginx-conf-parser)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
