如何对 Nginx 配置文件应用一致的缩进和格式?

Asa*_*ine 8 config nginx formatter

例如,我有这个混乱的配置:

server {
listen 80 default;
                        server_name localhost;
location / {
proxy_method $foo;
                    proxy_pass http://foobar:8080;
}
} 
Run Code Online (Sandbox Code Playgroud)

我想让它看起来像:

server
{
    listen 80 default;
    server_name localhost;
    location /
    {
        proxy_method $foo;
        proxy_pass http://foobar:8080;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何以更好的方式格式化 Nginx 配置?

vas*_*ich 6

有一些格式化程序,例如:

  1. 1connect 的Nginx Formatter ( python ),它有一个很好的本地可运行工具,效果非常好!
  2. Blindage.org 上的Nginx Formatter ( python ) 没有尝试过,但从他的示例输出来看似乎不错。
  3. Nginx Beautifier ( javascript ) 也可以在nginxbeautifier.com上作为一个小型 js 工具使用,就像jsbeautifier.com一样,当然也在github上开源,您也可以通过以下方式在本地运行它:

从 npm (nodejs 包管理器)安装它

npm install -G nginxbeautifier
Run Code Online (Sandbox Code Playgroud)

从 arch aur (arch 用户存储库)安装它

pacaur -S nginxbeautifier
Run Code Online (Sandbox Code Playgroud)

从 github 存储库gitgithub)克隆:

git clone https://github.com/vasilevich/nginxbeautifier.git
Run Code Online (Sandbox Code Playgroud)

执行 nginxbeautifier -h 或 nginxbeautifier --help 后,即可获得有关如何在本地使用该程序的说明,也可以在 github 页面本身上找到。


完全披露我是“nginxbeautifier.com”和相关 github 页面的开发者和维护者
,请报告那里的任何问题,
nginxbeautifier 中的一些代码实际上是受到提到的第一个选项的启发。