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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...