Skip to main content

nginx config file formatter/beautifier

nginx config file formatter/beautifier written in Python with no additional dependencies. It can be used as a library or a standalone script. It formats nginx configuration files in a consistent way, described below:

  • All lines are indented uniformly, with four spaces per level. The number of spaces is customizable.
  • Neighboring empty lines are collapsed to at most two. The number is customizable (see options below).
  • Curly brace placement follows the Java convention.
  • Whitespace is collapsed, except in comments and within quotation marks.
  • Newline characters are normalized to the operating system default (LF or CRLF), but this can be overridden.

Installation

Python 3.4 or later is needed to run this program. The easiest way is to download the package from PyPI:

pip install nginxfmt

Manual installation

The simplest form of installation is copying nginxfmt.py to your scripts' directory. It has no third-party dependencies.

You can also clone the repository and symlink the executable:

cd
git clone https://github.com/slomkowski/nginx-config-formatter.git
ln -s ~/nginx-config-formatter/nginxfmt.py ~/bin/nginxfmt.py

Usage as standalone script

It can format one or several files. By default, the result is saved to the original file, but it can be redirected to stdout. It can also function in piping mode, using the --pipe or - switch.

usage: nginxfmt.py [-h] [-v] [-] [-p | -b] [-i INDENT] [--max-empty-lines MAX_EMPTY_LINES] [--line-endings {auto,unix,windows,crlf,lf}] [config_files ...]

Formats nginx configuration files in consistent way.

positional arguments:
config_filesconfiguration files to format

options:
-h, --helpshow this help message and exit
-v, --verbose show formatted file names
-, --pipe reads content from standard input, prints result to stdout
-p, --print-resultprints result to stdout, original file is not changed
-b, --backup-original
backup original config file as filename.conf~

formatting options:
-i, --indent INDENT specify number of spaces for indentation
--max-empty-lines MAX_EMPTY_LINES
maximum number of consecutive empty lines to keep (default: 2)
--line-endings {auto,unix,windows,crlf,lf}
specify line ending style: 'unix' or 'lf' for \n, 'windows' or 'crlf' for \r\n. When not provided, system-default is used

Using as a library

The main logic is within the Formatter class, which can be used in third-party code.

import nginxfmt

# initializing with standard FormatterOptions
f = nginxfmt.Formatter()

# format from string
formatted_text = f.format_string(unformatted_text)

# format file and save result to the same file
f.format_file(unformatted_file_path)

# format file and save result to the same file, original unformatted content is backed up
f.format_file(unformatted_file_path, backup_path)

Customizing formatting options:

import nginxfmt

fo = nginxfmt.FormatterOptions()
fo.indentation = 2  # 2 spaces instead of default 4
fo.line_endings = '\n'  # force Unix line endings
fo.max_empty_lines = 1  # allow at most 1 consecutive empty line (default: 2)

# initialize with standard FormatterOptions
f = nginxfmt.Formatter(fo)

Reporting bugs

Please create an issue at https://github.com/slomkowski/nginx-config-formatter/issues. Be sure to include config snippets to reproduce the issue, preferably:

  • Snippet to be formatted
  • Actual result with the invalid formatting
  • Desired result

Credits

Copyright 2021 Michał Słomkowski. License: Apache 2.0. Previously published under https://github.com/1connect/nginx-config-formatter.

Metadata

Release files for nginxfmt 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nginxfmt 1.4.0
File Size Uploaded
nginxfmt-1.4.0.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nginxfmt 1.4.0
File Interpreter ABI Platform
nginxfmt-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / nginxfmt-1.4.0.tar.gz

Download URL nginxfmt-1.4.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e5dcb3e7e05eafd60a2fcb3fe896af538c94dfbde971c212e99178eb4e26d35f
BLAKE2b-256 checksum
How to use checksums
60241a1d7b1a153890f1b2d9a6e4e677471976d053409567c6e979a64a6c7150
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.2 Linux/6.12.63-1-lts

Release files / nginxfmt-1.4.0-py3-none-any.whl

Download URL nginxfmt-1.4.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
200c434fa97a90b5ecee9cf35ccfcfb596b15ff3d9ad3d3ce7006d444420b5b2
BLAKE2b-256 checksum
How to use checksums
65de2eeb85279374b6990898ac3d6909ab190b704c11131633a6272b5d340b42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.2 Linux/6.12.63-1-lts

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.0.6

1 release file

1.0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page