cancel
Showing results for 
Search instead for 
Did you mean: 

2.2.4 EE Nginx multi-website single domain all pointing to main store

2.2.4 EE Nginx multi-website single domain all pointing to main store

I've set up a fresh install of 2.2.4 EE (no 3rd party modules or anything yet) and I'm setting this up as a multi-website on a single domain.  Because this is eventually replacing an existing site, I've got it using the 'prod' subdomain for the main website, and each other website is on a sub-subdomain.  So here is an example of my setup:

 

prod.site.com

corpA.prod.site.com

corpB.prod.site.com

 

I've used Nginx and letsencrypt to setup each of the sites per the documentation, and in the admin store configuration, I've set all 4 base urls to match.  Here's an example nginx record

map $http_host $MAGE_RUN_CODE {
   corpA.prod.site.com corpa;
}

map $http_host $MAGE_RUN_TYPE {
   corpA.prod.site.com website;
}

server {
   server_name corpA.prod.site.com;
   set $MAGE_ROOT /var/www/html/magento2;
   include /var/www/html/magento2/nginx.conf.sample;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/corpA.prod.site.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/corpA.prod.site.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = corpA.prod.site.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


   listen 80;
   server_name corpA.prod.site.com;
    return 404; # managed by Certbot
}

I've also added the following in the nginx.conf.sample file as they mentioned in the docs

    fastcgi_param  MAGE_RUN_TYPE $MAGE_RUN_TYPE;
    fastcgi_param  MAGE_RUN_CODE $MAGE_RUN_CODE;

I do not have store SID turned on (user sharing is set to website level, we're not sharing users between sites), store code appended to path, or 302 redirect set up because I'd like the sub-subdomain listed in the url whenever you're on that site.

 

Going to my corpA.prod.site.com loads but shows content from the main website such as site name and homepage (and I'm getting cross origin errors as it attempts to load static content / customer info from the prod.site.com website).  The url is correct though, and its loading the right certificate.

 

What I'm looking to solve is 

1.) How can I get the correct site to load?  I believe I'm setting MAGE_RUN_CODE correctly and the url / cert is fine, its appears to allows load the main site though.

2.) How can I fix these cross origin errors?  I'm not sure why the static content is not respecting the base url.

 

I've seen some posts talking about modifying the index.php file, is this required?

1 REPLY

Re: 2.2.4 EE Nginx multi-website single domain all pointing to main store

Hi @joseph_digiovanna,

 

I didn't tried with Magento 2 yet but the idea should be the same (I guess).

You can use the map module of Nginx to set which domain witl exectue which Magento Store code.

 

Probably you won't be able to copy&paste but I hope to provide an idea.

 

Let's say you have a domain configured at:

 

/etc/nginx/sites-enables/yourdomain.com

Now edit the file and add something like this:

 

map $http_host $mage_run_code {
    first.yourdomain.com store_code_name_1;
    second.yourdomain.com  store_code_name_2;
}

Also, you will need to create another block for your second domain:

 

server {
    listen      80;
    server_name second.yourdomain.com;
 
    ...
 
}

There are antoher options there.

 

Finally you'll have something like:

 

location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php7.1-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 
    ...
 
}

So there you can add these params too:

 

fastcgi_param MAGE_RUN_TYPE website;
fastcgi_param MAGE_RUN_CODE $mage_run_code;

Probably something is not complete here (I've used an example from an old Magento 1 instance) but I guess you'll find the solution if you explore the map function.

 

(my 2 cents)

--
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"