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…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...