A command line tool for validating nginx files for style and common pitfalls.
- Node 18+
You can install nginx-linter globally using:
npm install -g nginx-linter
You can run nginx-linter without any configurations like so:
nginx-linter
This will by default validate the configuration files under /etc/nginx/
.
For all the options, you can use the --help
:
nginx-linter --help
There are a number of built in rules in nginx-linter designed both keep your nginx configuration style consist but also to find common pitfalls which may cause bugs in production.
The if
directive has a number of common pitfalls due to nginx configuration
being a declarative language not a procedural one and as a result should
either be used sparingly or not at all. There are two modes for this rule
which can be used:
- always - Don't allow any uses of the
if
directive regardless if they are a safe to use - mostly - Only allow uses of the
if
directive which are documented as safe to use
Default value: mostly
Enforces a consistent indentation for all directive blocks and lua blocks. If the configuration value
is a digit, it indicates the number of spaces to use for each level of indentation. Alternatively,
you can use tab
as the value to use tabs for indentation.
Default value: 4
Enforce a consistent line ending within the file. The configuration value can either be lf
for
Unix/Linux/MacOS style newlines or crlf
for Windows style newlines.
Default value: lf
This rules enforces that locations are ordered in the same order in which they will would take precedence when matching requests. A common pitfall is thinking that Nginx configuration is matched sequentially which isn't the case. By enforcing that locations are ordered in precedence order, this will lead to less surprising results in production.
location
directives have a number of modifiers which can be used change the way the location
matches. As these are special symbols it can be difficult for a novice to remember which modes
allow for regular expressions and which ones are regular string comparisons. strict-location
checks that prefix and exact matches don't use regular expressions in the location URI.
Enforce that no line ends with whitespace. This rule is largely for cleanliness and style purposes.