The End of Ye Olde Ugly NGinx Config Files

Written by James McDonald

June 16, 2017

Install python3. This is for CentOS


yum install python34

Download and install nginxfmt.py. The download and the instructions are at https://github.com/1connect/nginx-config-formatter

Once installed usage is as follows.


# do a backup
tar -czvf nginx-conf-backup.tar.gz /etc/nginx/conf.d

cd /etc/nginx/conf.d

# warning this does an inplace edit
nginxfmt.py *.conf

A word of warning. If you have code like the following which is using {} to escape a variable

if ( $remote_addr != 10.11.12.13 ) {
        set $check G;
}
if ( -f $document_root/maintenance.html){
                set $check "${check}O";
        }
if ( $check = GO ) {
        return 503;
}

It will screw it up because of the brackets as follows. But it isn’t a problem to manually fix it after the formatting is done. When you restart nginx it will complain of the bad format so you will know you need to look for this sort of gotcha

if ( -f $document_root/maintenance.html) {
        set $check "$ {
            check
        }
        O";
    }

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…