Reindent an Nginx server block without changing its arguments
Admin ·
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.
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.
| Line | 2 spaces | 4 spaces |
|---|---|---|
| events opening | 0 spaces | 0 spaces |
| location opening | 4 spaces | 8 spaces |
| add_header inside location | 6 spaces | 12 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.
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.