Welcome to Centmin Mod Community
Become a Member

Additions to custom_config.inc for persistent NGINX .conf

Discussion in 'Feature Requests & Suggestions' started by ethanpil, Dec 16, 2016.

  1. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    I would guess that most centminmod users are fairly experienced with linux/nginx/servers, etc, and its quite common to customize the nginx.conf files.

    Usually we append or prepend to the location{} command of vhosts in NGINX. Or maybe play with try_files commands, or even setup unique and favorite custom nginx configurations.

    It will be quite useful to make these customization more automated, persistent and reproducible.

    Some examples:

    1. Often, I manually create sites for wordpress Option 2 instead of 22 because I have my special caching and security settings and exceptions for WP.

    2. Often I install another PHP framework or application that has its own recommended NGINX directives.

    3. I am thinking of prepending try_files to check if there is a __BILLING.html file in the root folder which I am creating when account is past due (and deleting when paid), to make a simple billing enforcement for clients.

    Anyway I have many specialized kinds of servers with some unique commands always used in nginx and with centmin its hard to update without worry of losing all the extra work. Each new server is also a lot of configuration. I wish I can easily tell centminmod I always want it to add this to conf files when creating a new domain, or updating a conf file. Its easy to modify centmin source code but then its not persistent for updates and not easy to replicate across server.


    My idea is to add these optional settings to /etc/centminmod/custom_config.inc:

    Code:
    NGINX_LOCATION_PREPEND=filename1
    NGINX_LOCATION_APPEND=filename2
    NGINX_LOCATION_BEFORE=filename3
    NGINX_LOCATION_AFTER=filename4
    RUN_AFTER_NEWVHOST=filename1
    
    Lets see how this works:

    Code:
    NGINX_LOCATION_PREPEND=filename1
    
    When centminmod is writing the location{}  block of a new vhost, then prepend the contents of filename1  at the top of location{
    

    Code:
    NGINX_LOCATION_APPEND=filename1
    
    When centminmod is writing the location{}  block of a new vhost, then append the contents of filename1  at the end of  location{  }  --before the closing brace
    
    Code:
    NGINX_LOCATION_BEFORE=filename1
    
    When centminmod is writing the location{}  block of a new vhost, then include the file of filename1  before location{}
    
    Code:
    NGINX_LOCATION_AFTER=filename1
    
    When centminmod is writing the location{}  block of a new vhost, then include the file of filename1  after location{}
    
    Code:
    NGINX_LOCATION_AFTER=filename1
    
    When centminmod is writing the location{}  block of a new vhost, then include the file of filename1  after location{}
    
    This way its for anyone setup all common favorite nginx settings on servers and can easily replicate customization across them and can also upgrade without worry.

    I can easily setup my own default wordpress configurations and exceptions for plugins, or even custom try_files blocks if I want to get crazy.

    Bonus :)
    To go even farther, I would love to be able to execute a custom bash script at the end of vhost creation with vars accessible to the script like vhostname and vhostpath . This can be used if I want to setup my own notifications or databases, files, frameworks, etc...

    Code:
    RUN_AFTER_NEWVHOST=filename1
    
    So, with these functions for all new servers we can install as usual and then simply upload our unique customizations /etc/centminmod or even /etc/centminmod/custom and then all customization will be persistent and easily reproducible. It can even become a way to create 'plugins'.

    I would love to see these power tools added to the incredible system, it will really make persistent customization a breeze.
     
    Last edited: Dec 16, 2016
  2. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  3. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    As you see, in the original thread I have the same request... :)
    This variation is more useful and generic... Would make it very easy to automate deployment of CMM...
     
  4. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    Maybe I am silly. Isn't this just a matter of editing two files?

    centmin.sh #at the top where setting defaults

    Code:
    #Dont include additional NGINX conf directives by default
    NGINX_LOCATION_PREPEND=''
    NGINX_LOCATION_APPEND=''
    NGINX_LOCATION_BEFORE=''
    NGINX_LOCATION_AFTER=''
    RUN_AFTER_NEWVHOST=''
    

    inc/nginx_addvhost.inc - At top:
    Code:
    #load custom NGINX configuration content
    
    if [ "${NGINX_LOCATION_PREPEND}" != '' ] && [ -f "${CONFIGSCANBASE}/{NGINX_LOCATION_PREPEND}" ];
    then 
       NGINX_LOCATION_PREPEND_CONTENT=$( cat {NGINX_LOCATION_PREPEND} )
    else 
       NGINX_LOCATION_PREPEND_CONTENT=''
    fi
    
    if [ "${NGINX_LOCATION_APPEND}" != '' ] && [ -f "${CONFIGSCANBASE}/{NGINX_LOCATION_APPEND}" ];
    then 
       NGINX_LOCATION_APPEND_CONTENT=$( cat {NGINX_LOCATION_APPEND} )
    else 
       NGINX_LOCATION_APPEND_CONTENT=''
    fi
    
    if [ "${NGINX_LOCATION_BEFORE}" != '' ] && [ -f "${CONFIGSCANBASE}/{NGINX_LOCATION_BEFORE}" ];
    then 
       NGINX_LOCATION_BEFORE_CONTENT=$( cat {NGINX_LOCATION_BEFORE} )
    else 
       NGINX_LOCATION_BEFORE_CONTENT=''
    fi
    
    if [ "${NGINX_LOCATION_AFTER}" != '' ] && [ -f "${CONFIGSCANBASE}/{NGINX_LOCATION_AFTER}" ];
    then 
       NGINX_LOCATION_AFTER_CONTENT=$( cat {NGINX_LOCATION_AFTER} )
    else 
       NGINX_LOCATION_AFTER_CONTENT=''
    fi
    
    Then 123.09beta01/inc/nginx_addvhost.inc

    Add in line 648, and add in line 746
    Code:
    $NGINX_LOCATION_BEFORE_CONTENT
    
    Add in line 648, add in line 765
    Code:
    $NGINX_LOCATION_AFTER_CONTENT
    
    Add in line 650, and add in line 748
    Code:
    $NGINX_LOCATION_PREPEND_CONTENT
    
    Add in line 665, add in line 763
    Code:
    $NGINX_LOCATION_APPEND_CONTENT
    
    Add in Line 533
    Code:
    if [ "${RUN_AFTER_NEWVHOST}" != '' ] && [ -f "${CONFIGSCANBASE}/{RUN_AFTER_NEWVHOST}" ];
    then
       source RUN_AFTER_NEWVHOST
    fi
    
     
  5. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    maybe looks good but as I've been sick for past 3 weeks and still sick it's not the time to test and look into yet :)
     
    Last edited: Dec 20, 2016
  6. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    2 line 648s ?
     
  7. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    Of course mate! Feel better. I was just trying to verify my thought. I hope we can test it once you feel better...

    Sorry about that. The second 648 should be 675. Seems I can no longer edit the original post...

    Add in line 648, and add in line 746
    Code:
    $NGINX_LOCATION_BEFORE_CONTENT
    
    Add in line 675, add in line 765
    Code:
    $NGINX_LOCATION_AFTER_CONTENT
    
     
  8. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    Hope you are feeling better?Any chance we can test this soon? :)
     
  9. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    not yet :)
     
  10. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    Just checking in on my dreams... o_O:)
     
  11. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Not yet :)

    Probably won't be until after 123.09beta01 goes stable.
     
    Last edited: Feb 1, 2017
  12. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    I was recently thinking that instead of the above complex scenario, it may be wiser and easier to simply store the entire location{} directive in an external file, and include that within nginx_addvhost.inc

    So, instead of append and prepend in location{} as I described above, we move the default location{} code to /inc/nginx_addvhost_location.inc and/or /inc/nginx_addvhost_location_wp.inc as you see fit.

    Then we can just have a default setting in /etc/centminmod/custom_config.inc like this

    Code:
    NGINX_LOCATION_CUSTOM=N
    Then in inc/nginx_addvhost.inc

    Code:
    #load custom NGINX configuration content
    
    if [ "${NGINX_LOCATION_CUSTOM}" != 'N' ] && [ -f "${CONFIGSCANBASE}/{NGINX_LOCATION_CUSTOM}" ];
    then
       source "${CONFIGSCANBASE}/{NGINX_LOCATION_CUSTOM}"
    else
       source "/inc/nginx_addvhost_location.inc"
    fi
    We can base our code of the default /inc/nginx_addvhost_location.inc that you provide, or customize as needed.

    What do you think about this simpler and more powerful method?
     
  13. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Yeah that is much simpler though you would also have to pay attention to order of location contexts throughout the vhost. Whatever method we decide on, needs alot of testing first too :)
     
  14. ethanpil

    ethanpil Active Member

    173
    55
    28
    Nov 8, 2015
    Ratings:
    +101
    Local Time:
    9:56 AM
    I'm excited to test, when the time comes. :whistle:
     
  15. eva2000

    eva2000 Administrator Staff Member

    54,527
    12,211
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,780
    Local Time:
    9:56 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+