Join the community today
Become a Member

Nginx Redirect in nginx_addvhost.inc

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

  1. nVidian

    nVidian Member

    92
    8
    8
    Mar 16, 2015
    Ratings:
    +25
    Local Time:
    8:16 AM
    1.7.9
    5.5
    Please fill in any relevant information that applies to you:
    • CentOS Version: 7
    • Centmin Mod Version Installed: 123.09beta01
    • Nginx Version Installed: 1.13.9
    • PHP Version Installed: i.e. 5.6.30 or 7.0.15
    • MariaDB MySQL Version Installed: 10.1.31
    • When was last time updated Centmin Mod code base ? : i.e. run centmin.sh menu option 23 submenu option 2
    • Persistent Config: Do you have any persistent config file options set in /etc/centminmod/custom_config.inc ? You can check via this command:


      cat /etc/centminmod/custom_config.inc


      Code:
      NGINX_PAGESPEED=y
      NGINX_ZLIBCUSTOM='y'
      ORESTY_LUANGINX=n
      NGINX_XSLT='n'
      NGINX_LIBBROTLI='y'
      NGXDYNAMIC_XSLT='n'
      NGXDYNAMIC_IMAGEFILTER='y'
      NGXDYNAMIC_GEOIP='y'
      NGXDYNAMIC_STREAM='y'
      NGXDYNAMIC_HEADERSMORE='y'
      NGXDYNAMIC_SETMISC='y'
      NGXDYNAMIC_ECHO='y'
      NGXDYNAMIC_SRCCACHE='y'
      NGXDYNAMIC_MEMC='y'
      NGXDYNAMIC_REDISTWO='y'
      NGXDYNAMIC_NGXPAGESPEED='y'
      NGXDYNAMIC_BROTLI='y'
      PHPMSSQL='y'
      PHP_PGO='y'
      PHP_PGO_CENTOSSIX='y'
      NGINX_DEVTOOLSETGCC='y'
      GENERAL_DEVTOOLSETGCC='y'
      CLANG='n'
      LIBRESSL_SWITCH='n'
      NGX_GSPLITDWARF='y'
      PHP_GSPLITDWARF='y'
      NGX_LDGOLD='y'
      LETSENCRYPT_DETECT='y'
      PUREFTPD_DISABLED='y'
      

    Hello, I want to insert this redirect in nginx_addvhost.inc

    Code:
            #Redirect to www 
        if ($host ~* ^mydomain\.tld$) {
            rewrite ^(.*)$ https://www.mydomain.tld$1 permanent;
        }
    
    OR 
    
        #Redirect to non www
          if ($host ~* ^www\.mydomain\.tld$) {
            rewrite ^(.*)$ https://mydomain.tld$1 permanent;
        }
    But centminmode always delete the "$host" part so it became
    Code:
            #Redirect to www 
        if ( ~* ^mydomain\.tld$) {
            rewrite ^(.*)$ https://www.mydomain.tld$1 permanent;
        }
    
    OR 
    
        #Redirect to non www
          if ( ~* ^www\.mydomain\.tld$) {
            rewrite ^(.*)$ https://mydomain.tld$1 permanent;
        }
    How do I prevent centminmod to delete the "$host" ?
     
  2. eva2000

    eva2000 Administrator Staff Member

    50,906
    11,799
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,256
    Local Time:
    11:16 AM
    Nginx 1.25.x
    MariaDB 10.x
    I wouldn't edit nginx_addvhost.inc directly as Centmin Mod updates will overwrite that nginx_addvhost.inc include file on each update. You loose the $host part because the vhost template is within a template which needs you to escape any variable references with $ in front that you want in final version of vhost template, so $host needs to be references as \$host if you want it to end up as $host in vhost final generated template.

    Some custom rules might be able to use the new pre-static include files I added Beta Branch - add VHOST_PRESTATICINC='y' additional include files though the order of the rules might be important.