Welcome to Centmin Mod Community
Become a Member

Nginx How to handle sub-domain?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by Afaq, Feb 22, 2018.

  1. Afaq

    Afaq Member

    81
    3
    8
    Aug 5, 2015
    Ratings:
    +5
    Local Time:
    7:30 PM
    I am using Centminmod on CentOS 7 and Magento 2.2.2. I added the vhost for my domain and setup the website as normal. I am currently doing the hosts file hack to access it. The website doesnt go live until the clients approve it. So I want to put it live as demo.actualdomain.com. Such that the current website is still accessible at www.actualdomain.com.

    How can I achieve this? I understand that I will have to do some changes in the vhost nginx config file and add a DNS entry. Can anyone guide exactly what changes and entry I will have to make?


    This is my current vhost config:

    Code:
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    #server {
    #            listen   80;
    #            server_name domain.com;
    #            return 301 $scheme://www.domain.com$request_uri;
    #       }
    
    server {
     
      server_name domain.com www.domain.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      set $MAGE_ROOT /home/nginx/domains/domain.com/public;
      set $MAGE_MODE default;
      charset off;
      #add_header X-Frame-Options SAMEORIGIN;
      add_header X-Xss-Protection "1; mode=block" always;
      add_header X-Content-Type-Options "nosniff" always;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/domain.com/log/access.log main_ext buffer=256k flush=60m;
      error_log /home/nginx/domains/domain.com/log/error.log;
    
      #include /usr/local/nginx/conf/autoprotect/domain.com/autoprotect-domain.com.conf;
      #root /home/nginx/domains/domain.com/public;
      root $MAGE_ROOT/pub;
    
      # uncomment cloudflare.conf include if using cloudflare for
      # server and/or vhost site
      #include /usr/local/nginx/conf/cloudflare.conf;
      #include /usr/local/nginx/conf/503include-main.conf;
    
    location /setup {
        root $MAGE_ROOT;
        location ~ ^/setup/index.php {
            include /usr/local/nginx/conf/php.conf;
        }
     
        location ~ ^/setup/(?!pub/). {
            deny all;
        }
     
        location ~ ^/setup/pub/ {
            add_header X-Frame-Options "SAMEORIGIN";
        }
    }
     
    location /update {
        root $MAGE_ROOT;
     
        location ~ ^/update/index.php {
            fastcgi_split_path_info ^(/update/index.php)(/.+)$;
            include /usr/local/nginx/conf/php_magento.conf;
        }
     
        # deny everything but index.php
        location ~ ^/update/(?!pub/). {
            deny all;
        }
     
        location ~ ^/update/pub/ {
            add_header X-Frame-Options "SAMEORIGIN";
        }
    }
    
      location / {
      #include /usr/local/nginx/conf/503include-only.conf;
      # block common exploits, sql injections etc
      #include /usr/local/nginx/conf/block.conf;
    
      try_files $uri $uri/ /index.php?$args;
      }
    
    location /pub {
        location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
            deny all;
        }
        alias $MAGE_ROOT/pub;
        add_header X-Frame-Options "SAMEORIGIN";
    }
     
    location /static/ {
        if ($MAGE_MODE = "production") {
            expires max;
        }
        location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
            add_header Cache-Control "public";
            add_header X-Frame-Options "SAMEORIGIN";
            expires +1y;
     
            if (!-f $request_filename) {
                rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
            }
        }
        location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
            add_header Cache-Control "no-store";
            add_header X-Frame-Options "SAMEORIGIN";
            expires    off;
     
            if (!-f $request_filename) {
               rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
            }
        }
        if (!-f $request_filename) {
            rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        }
        add_header X-Frame-Options "SAMEORIGIN";
    }
     
    location /media/ {
        try_files $uri $uri/ /get.php?$args;
     
        location ~ ^/media/theme_customization/.*\.xml {
            deny all;
        }
     
        location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
            add_header Cache-Control "public";
            add_header X-Frame-Options "SAMEORIGIN";
            expires +1y;
            try_files $uri $uri/ /get.php?$args;
        }
        location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
            add_header Cache-Control "no-store";
            add_header X-Frame-Options "SAMEORIGIN";
            expires    off;
            try_files $uri $uri/ /get.php?$args;
        }
        add_header X-Frame-Options "SAMEORIGIN";
    }
     
    location /media/customer/ {
        deny all;
    }
     
    location /media/downloadable/ {
        deny all;
    }
     
    location /media/import/ {
        deny all;
    }
     
    location ~ cron\.php {
        deny all;
    }
     
    location ~ (index|get|static|report|404|503)\.php$ {
        fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_param  PHP_VALUE "memory_limit=256M \n max_execution_time=600";
        fastcgi_param  MAGE_MODE $MAGE_MODE;
        include /usr/local/nginx/conf/php.conf;
    }
    
      #include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
     
  2. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    12:30 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    You basically want to park a domain on top of another or a domain alias where 2 domains/subdomains both resolve and point to the same site and site web root ?

    For that there's a nice thread at Domains - Usage of a Short Domain | Centmin Mod Community

    DNS Domain Parking



    First step is at DNS level, ensure you domains point to same server IP address for both live domain and test subdomain/domain to be parked. You may need up to 24-48hrs to see domain propagation and can use online too at whatsmydns.net to check your domains DNS A records.

    Nginx Domain Parking/Alias



    Then, you basically edit the domain's nginx vhost /usr/local/nginx/conf/conf.d/domain.com.conf file and/or /usr/local/nginx/conf/conf.d/domain.com.ssl.conf if it exists and edit the server_name listing so if abc.com was real live site and xyz.com is the domain your want to park on top of abc.com as domain alias, it would be
    Code (Text):
    server {
    server_name abc.com xyz.com;

    If you need www too it would be
    Code (Text):
    server {
    server_name abc.com www.abc.com xyz.com www.xyz.com;

    Then to ensure your SEO isn't messed up setup canonical link headers in nginx vhost file too as outlined here. You need to set it up in 2 places.

    In nginx vhost file itself i.e. /usr/local/nginx/conf/conf.d/abc.com.conf or /usr/local/nginx/conf/conf.d/domain.com.conf and if applicable /usr/local/nginx/conf/conf.d/abc.com.ssl.conf or /usr/local/nginx/conf/conf.d/domain.com.ssl.conf
    Code (Text):
    add_header Link "<http://abc.com$request_uri>; rel=\"canonical\"";
    

    and in /usr/local/nginx/conf/staticfiles.conf include file which is included in each nginx vhost file. Add header canonical link to the html location context
    Code (Text):
        location ~* \.(html|htm|txt)$ {
      add_header Link "<http://abc.com$request_uri>; rel=\"canonical\"";
      #add_header Pragma public;
      #add_header X-Frame-Options SAMEORIGIN;
      #add_header X-Xss-Protection "1; mode=block" always;
      #add_header X-Content-Type-Options "nosniff" always;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        access_log off;
        expires 1d;
        break;
            }
    

    This is so that html based files also serve the correct canonical link header to tell search engines which is the real domain so that search engines do not see it as duplicate content.

    Then restart Nginx and PHP-FPM at same time using Centmin Mod command shortcuts outlined in FAQ item 15
    Code (Text):
    nprestart
    

    To confirm just run curl command in SSH against your domain i.e. curl headers and grep filter for word canonical
    Code (Text):
    curl -sI https://abc.com | grep canonical
    

    output would be something like
    Code (Text):
    curl -sI https://abc.com | grep canonical
    Link: <http://abc.com/>; rel="canonical"
    
     
  3. Afaq

    Afaq Member

    81
    3
    8
    Aug 5, 2015
    Ratings:
    +5
    Local Time:
    7:30 PM
    Thanks for the detailed response.

    I think I wasnt clear in my initial question. There arent two domains involved. There is only one domain (lets call it abc.com). It already directs to the old website on some other server. I dont want to change that until the new website is approved. I just want demo.abc.com to direct towards the centminmod server with the new website. And once ultimately its approved, I would want to put it live at abc.com.

    This is what I want:
    abc.com--->old server, old website
    demo.abc.com--->centminmod server, new website
     
  4. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    12:30 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    doesn't matter if it's 2 domains or same domain using subdomain, the process for parking a domain on the same site/IP is the same as outlined above.

    just for abc.com domain don't update DNS to centmin mod server's IP to keep it on old server for time being.
     
  5. Afaq

    Afaq Member

    81
    3
    8
    Aug 5, 2015
    Ratings:
    +5
    Local Time:
    7:30 PM
    Ok so I did this:
    Code:
    server {
    server_name demo.abc.com www.demo.abc.com;
    On the domain end, I added a new A record with centmin server IP as value and 'demo' as value. However, it first goes to the centmin server but without anything opening, it redirects to www.abc.com and the actual live website opens up.
     
  6. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    12:30 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    probably some setting in your private centmin mod copy of your web app has registered official url as www.abc.com so when you visitr the private copy of your web app it goes there. You should edit your centmin mod's web app to regiser the demo domain as default probably. Best to contact that web app's developers/support to confirm.