Configuration guides

Reindent an Nginx server block without changing its arguments

·

Indentation helps you follow an Nginx block, while the argument's contents can affect how a directive behaves. The worked example gives you a way to verify both.

Nginx Formatter

Reindent the nested server and location

Select Load example and Format configuration with 2 spaces. The result has four blocks, including events, http, server and location. The location directive begins with four spaces. The directives inside location begin with six spaces.

Change Indentation to 4 spaces and format the same source. The location directive now begins at eight spaces. Its add_header directive begins at twelve spaces. Compare the line contents at both settings.

Reindent the nested server and location
Line2 spaces4 spaces
events opening0 spaces0 spaces
location opening4 spaces8 spaces
add_header inside location6 spaces12 spaces

Nginx configuration syntaxNginx 1.28 configuration tokenizer

Compare the arguments and comments

Find the argument "report #1; {draft}". The hash, semicolon and braces inside its quotes stay in the same argument. The separate comment # response label stays with the directive.

Check the literal blue#green, the escaped hash and ${uri}/ in the result. The formatter does not need variable expansion to indent the block.

Nginx 1.28 configuration tokenizer

Open the formatted file and compare include paths

Download nginx-formatted.conf and reopen it as text. Confirm that include conf.d/*.conf; still points to the same glob and that the adjacent comment is present.

The browser only formats the supplied text. Review the referenced configuration and its behavior on the target Nginx installation before using the new file.

Nginx core include and events directivesNginx command-line options

Common questions

Do inline directives stay on one line?

Complete directives are placed on separate lines so their nesting is visible. Inline comments follow the token they originally accompanied.

Does the formatter change a multiline quoted argument?

The text inside a quoted argument stays in place, including spaces and newlines. The surrounding directive is indented separately.

Sources

Put it into practice.

Related guides