Join the community today
Become a Member

Nginx How to edit vhost template ?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by nVidian, Mar 18, 2015.

Tags:
  1. nVidian

    nVidian Member

    89
    8
    8
    Mar 16, 2015
    Ratings:
    +23
    Local Time:
    11:25 PM
    1.7.9
    5.5
    Hi, how & where to edit the default nginx vhost template so I dont have to edit every vhost after create using menu #2 ?
     
  2. Mask

    Mask Active Member

    104
    29
    28
    Nov 10, 2014
    Ratings:
    +35
    Local Time:
    9:25 PM
    Nginx 1.9.1
    MariaDB 10.0.19
    I am afraid you will have to edit the config file for each domain since each domain has it's own config file. You can view them under /usr/local/nginx/conf/conf.d/

    If there is come configuration that you need to add to all domains, add it in a file and then just include the file with one line in each domain config.
     
  3. eva2000

    eva2000 Administrator Staff Member

    33,733
    7,467
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,480
    Local Time:
    2:25 AM
    Nginx 1.13.x
    MariaDB 5.5
    actually default vhost template for menu option 2 in centmin.sh is at inc/nginx_addvhost.inc

    For .07 stable it's at lines 47 to 93 of inc/nginx_addvhost.inc

    although include file method might be better depending on what you are trying to do :)
     
  4. neverminder

    neverminder Member

    31
    3
    8
    Nov 23, 2017
    Ratings:
    +3
    Local Time:
    7:25 PM
    1.13.6
    10.0.33
    If i edit the above file it will be overwritten by an nginx update, right? I'm just trying to find a solution to uncomment #include /usr/local/nginx/conf/cloudflare.conf; for every new vhost. Would that be possible?
     
  5. eva2000

    eva2000 Administrator Staff Member

    33,733
    7,467
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,480
    Local Time:
    2:25 AM
    Nginx 1.13.x
    MariaDB 5.5
    yes it would get overwitten if you edit default the template itself

    yes very easy if you know some shell scripting to uncomment all vhosts cloudflare.conf include file you just use a bash loop like this

    this first command just does a recursive grep returns which vhost files have the mere mention of the keyword cloudflare.conf include and on which line within each vhost file it is on within vhost directory at /usr/local/nginx/conf/conf.d/. When you have many vhost files in /usr/local/nginx/conf/conf.d/, the output would be more than one vhost file
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    

    example line 60 and 62 of /usr/local/nginx/conf/conf.d/domain.com.ssl.conf contain keywords for cloudflare.conf - first line is just a comment of instructions and second line is actual include file
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:60:  # uncomment cloudflare.conf include if using cloudflare for
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:62:  #include /usr/local/nginx/conf/cloudflare.conf;
    

    now using a shell for loop you can do a sed replacement replacing
    Code (Text):
    #include /usr/local/nginx/conf/cloudflare.conf;
    

    with
    Code (Text):
    include /usr/local/nginx/conf/cloudflare.conf;
    

    in SSH, run these 3 command for sed replacement - first command backups up vhost directory just in case. Forward slashes get escaped with back slash for sed.
    Code (Text):
    cp -a /usr/local/nginx/conf/conf.d /usr/local/nginx/conf/conf.d-backup-b4
    cd /usr/local/nginx/conf/conf.d/
    for v in $(ls /usr/local/nginx/conf/conf.d/); do sed -i "s|#include \/usr\/local\/nginx\/conf\/cloudflare.conf;|include \/usr\/local\/nginx\/conf\/cloudflare.conf;|g" $v; done
    

    Then use previous grep to check if the include file has been commented out
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    

    as you can see it has
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:60:  # uncomment cloudflare.conf include if using cloudflare for
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:62:  include /usr/local/nginx/conf/cloudflare.conf;
    

    When you have many vhost files in /usr/local/nginx/conf/conf.d/, the output would be more than one vhost file

    But this only is for after a vhost is created not during creation. I can easily add a persistent /etc/centminmod/custom_config.inc variable to control if vhosts are created with cloudflare.conf include enabled or disabled at vhost creation time. I'll have this added to 123.09beta01 branch within a few minutes/hours if you want to wait :)
     
  6. eva2000

    eva2000 Administrator Staff Member

    33,733
    7,467
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,480
    Local Time:
    2:25 AM
    Nginx 1.13.x
    MariaDB 5.5
    @neverminder

    you might want to use this newly added variable VHOSTCTRL_CLOUDFLAREINC to enable csfcf.sh cloudflare.conf include in newly created Nginx vhost config files as outlined at csfcf.sh - automate Cloudflare Nginx & CSF Firewall setups. In persistent config file /etc/centminmod/custom_config.in set
    Code (Text):
    VHOSTCTRL_CLOUDFLAREINC='y'
    

    Once set, just create nginx vhost normally via centmin.sh menu option 2, 22 or nv command.

    You would of needed to update Centmin Mod 123.09beta01 code first via SSH command
    Code (Text):
    cmupdate
    

    or via centmin.sh menu option 23 submenu option 2 then exit centmin.sh

    Full details at Beta Branch - add VHOSTCTRL_CLOUDFLAREINC & VHOSTCTRL_AUTOPROTECTINC variables
     
    • Like Like x 1
  7. neverminder

    neverminder Member

    31
    3
    8
    Nov 23, 2017
    Ratings:
    +3
    Local Time:
    7:25 PM
    1.13.6
    10.0.33
    Wow, that was fast! Thank you.
     
..