Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 247
Asaf Marine Avatar asked Aug 27 '16 23:08

Asaf Marine


1 Answers

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.

like image 174
vasilevich Avatar answered Oct 31 '22 14:10

vasilevich