Nginx config formatter

Indent and tidy an nginx.conf or a server block, one directive per line, with comments and quoted strings untouched.

loading Nginx config formatter
About this tool

What does an nginx config formatter do?

An nginx.conf grows by copy and paste. A server block from a blog post, a location from a colleague, a one-line upstream from a forum answer: soon the file has mixed indentation, three directives on one line and braces nobody can match by eye. nginx does not care, but you do, because every change is a risk when you cannot see which block a line belongs to. This formatter indents the file by block depth, puts one directive per line and leaves everything else alone.

Only the layout changes. Every word, quoted string, regular expression and $variable stays exactly as written, and the formatted file has the same tokens as the original. Comments stay: a comment on its own line stays at the level of the block, and a comment after a directive stays on that line. Quoted strings keep their spaces, quotes and braces, and ${variable} stays one word. Lua blocks from OpenResty, such as content_by_lua_block, are copied as they are and only re-indented. Formatting is idempotent: if you format the result again, nothing changes.

The formatter also finds the mistakes that stop nginx from starting: braces that are never closed, a closing brace with no opening one, a quote that is never closed and a directive without its semicolon, each with its line and column. It does not check directive names or values, so run nginx -t before you reload. Everything runs in your browser and your configuration, which may hold internal host names and paths, is not uploaded. Once the file is tidy, you can protect a location with a password file from the htpasswd generator.

How to format an nginx config

  1. Paste your nginx.conf, or just one server block, in the box. It opens with a messy example, so you see the result at once.
  2. The formatted file appears as you type. Choose the indent: 2 spaces, 4 spaces or a tab.
  3. Choose whether to keep blank lines. With Keep blank lines on, runs of blank lines are reduced to one. Turn it off to remove them all.
  4. Turn on Minify to get the opposite: no comments, no blank lines and each block on one line. The indent choice is disabled while Minify is on.
  5. If something is wrong, a red message under the result says what and where, for example "line 2, column 12". The output is still produced so you can see where the file stops making sense.
  6. Copy the result, or download it as nginx.conf.

For other files in the same project, the Dockerfile linter checks the image that runs nginx, and the YAML formatter tidies a compose or Kubernetes file.

Examples

The default example

Input, written the way it often arrives:

user www-data;worker_processes auto;
events{worker_connections 1024;}
http{
include mime.types;default_type application/octet-stream;
upstream app{server 127.0.0.1:3000 weight=2;server 127.0.0.1:3001 backup;}
server{listen 80;server_name example.com www.example.com;  # public site

# send everything to https
location / {return 301 https://$host$request_uri;}
location ~* \.(jpg|png|webp)$ {expires 30d;add_header Cache-Control "public, max-age=2592000";}
location /api/ {proxy_pass http://app;proxy_set_header Host $host;
if ($request_method = POST){return 405;}}
}
}

Output with 4 spaces:

user www-data;
worker_processes auto;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    upstream app {
        server 127.0.0.1:3000 weight=2;
        server 127.0.0.1:3001 backup;
    }
    server {
        listen 80;
        server_name example.com www.example.com; # public site

        # send everything to https
        location / {
            return 301 https://$host$request_uri;
        }
        location ~* \.(jpg|png|webp)$ {
            expires 30d;
            add_header Cache-Control "public, max-age=2592000";
        }
        location /api/ {
            proxy_pass http://app;
            proxy_set_header Host $host;
            if ($request_method = POST) {
                return 405;
            }
        }
    }
}

The regex in the location ~* line, the $host variables and the quoted header value are untouched. The comment after server_name stays on its line, with one space before it.

Two spaces, or minified

With an indent of 2 the first lines become:

user www-data;
worker_processes auto;
events {
  worker_connections 1024;
}
http {
  include mime.types;
  default_type application/octet-stream;

With Minify on, comments and blank lines go and each block shares one line:

user www-data;
worker_processes auto;
events {worker_connections 1024;}
http {include mime.types;default_type application/octet-stream;upstream app {server 127.0.0.1:3000 weight=2;server 127.0.0.1:3001 backup;}server {listen 80;server_name example.com www.example.com;location / {return 301 https://$host$request_uri;}location ~* \.(jpg|png|webp)$ {expires 30d;add_header Cache-Control "public, max-age=2592000";}location /api/ {proxy_pass http://app;proxy_set_header Host $host;if ($request_method = POST) {return 405;}}}}

A missing semicolon

Input:

server {
  listen 80
}

The formatter writes listen 80; in the output and reports line 2, column 12: listen does not end with a semicolon. nginx needs a ; at the end of every directive. Add the semicolon to your own file too.

Unclosed braces

http {
  server {
    listen 80;

Both braces are reported: line 1, column 6 and line 2, column 10, each with "This { is never closed. Add the matching } or remove it." Add the two closing braces at the end.

Common errors and how to fix them

  • This { is never closed: a block was opened and not closed. The line and column point at the opening brace, so search from there.
  • This } has no { before it: there is one closing brace too many. Remove it, or add the opening brace that is missing.
  • This quote is never closed: a string started with " or ' and never ended. Add the closing quote. Because everything after an open quote is part of the string, this error can hide others.
  • {directive} does not end with a semicolon: every simple directive ends with ;. Blocks, such as server { ... }, do not.
  • Unexpected token here: a stray ; or brace where a directive name should be, such as a 1;;. Remove it.

If the formatted file looks wrong, read the first error only. Fix it, and the others may disappear. The tool formats the structure, so a valid layout does not mean a valid configuration. Run nginx -t to check directive names and values. If a location returns a status code you do not remember, the HTTP status codes list explains it.

Frequently asked questions

How do I format an nginx.conf?

Paste the file, pick the indent and copy the result. Blocks are indented, each directive gets its own line and the words stay as written.

Does formatting change how nginx behaves?

No. Only spaces, tabs and line breaks change, and the formatted file has the same tokens as the original. Quoted strings, regular expressions and variables are kept exactly. Minifying removes comments, which nginx ignores anyway.

Does it work with OpenResty and Lua?

Yes. Lua blocks such as content_by_lua_block are copied with their braces and quotes and only re-indented, so Lua code is not mistaken for nginx directives.

Can it check my configuration?

It finds unbalanced braces, unclosed quotes and missing semicolons. It does not know which directives are valid in which context. Run nginx -t for that.

Is my configuration uploaded?

No. The formatting runs in your browser.

How to run the result

Bash

nginx -t                    # check the syntax with nginx itself
nginx -s reload             # reload without dropping connections
sudo systemctl reload nginx # the same, on a systemd host
Report a problem