Asaf Marine
Asaf Marine

Reputation: 393

How can I apply consistent indentation and formatting to Nginx config files?

I have got this messy config for example:

server {
listen 80 default;
                        server_name localhost;
location / {
proxy_method $foo;
                    proxy_pass http://foobar:8080;
}
} 

and I would like to make it look like:

server
{
    listen 80 default;
    server_name localhost;
    location /
    {
        proxy_method $foo;
        proxy_pass http://foobar:8080;
    }
}

How can I format Nginx configurations in a better way?

Upvotes: 8

Views: 5744

Answers (2)

vasilevich
vasilevich

Reputation: 753

There are a few formatters out there, such as:

  1. Nginx Formatter (python) by 1connect which has a nice locally runable tool, works very good!
  2. Nginx Formatter (python)at blindage.org , didnt try that one but it seems good by his example outputs.
  3. Nginx Beautifier(javascript) also available at nginxbeautifier.com as a tiny js tool just like jsbeautifier.com and of course also open source on github you can run it locally too by:

installing it from npm(nodejs package manager):

npm install -G nginxbeautifier

installing it from arch aur (arch user repository):

pacaur -S nginxbeautifier

cloning from by github repository(git and github):

git clone https://github.com/vasilevich/nginxbeautifier.git

instructions on how to use the program locally are available once you execute nginxbeautifier -h or nginxbeautifier --help and also on the github page itself.

Full disclosure I am the developer and the maintainer of "nginxbeautifier.com"
and the relevant github page
please report any issuses there,
some of the code in nginxbeautifier was acctualy inspired by the first option mentioned.

Upvotes: 7

Marcin
Marcin

Reputation: 1494

Any online / text editor's code beautifier should do the work, experiment with different languages to get the perfect indentations.

http://jsbeautifier.org/

Sample output from above website:

server {
    listen 80
    default;
    server_name localhost;
    location / {
        proxy_method $foo;
        proxy_pass http: //foobar:8080;
    }
}

Upvotes: 0

Related Questions