2

I'm currently trying to set up a Magento installation with separate store views for localization. However, I'm not sure the way I've currently implemented URL handling in Nginx is the cleanest solution.

The URL structure is:

http://www.example.com/        (US store)
http://www.example.com/nl/     (Dutch store)
http://www.example.com/nl/en   (Dutch store, English language)

and so on. The PHP code for each store view will be served from /var/www/magento/ regardless of URL structure (i.e. http://www.example.com/nl/en/index.php and http://www.example.com/index.php both correspond to /var/www/magento/index.php).

We're looking at 16 separate Magento stores right now, and might expand that to upwards of 30 eventually. Magento/PHP performance is a concern with this many stores, but I'm more worried about Nginx at this point.

Currently port 80 handles all incoming connections and based on the localization code uses proxy_pass to redirect them to a separate Nginx server on localhost. This server then sets the MAGE_RUN_CODE variable to specify the store code to Magento:

server {
    listen 80 default;
    server_name www.example.com;
    root /var/www/magento;

    location /nl {
        rewrite           ^/nl/(.*) /$1 break;                               
        proxy_pass        http://localhost:82;                                  
        proxy_set_header  X-Real-IP  $remote_addr;
    }

    ... ## Similar location blocks for other country codes

    location ^~ /media/ {                                                    
        expires 30d; ## Assume all files are cachable                       
    }

    ... ## Similar location blocks for other static content  

    location / {                                                                
        proxy_pass        http://localhost:81;                                  
        proxy_set_header  X-Real-IP  $remote_addr;                              
    }
}

server {
    listen localhost:81;
    server_name www.example.com;
    root /var/www/magento;

    ...

    # Handle PHP files
    location ~ .php$ {

        ... ## Uses standard Magento configuration, except for:

        fastcgi_param  MAGE_RUN_CODE en_us;
        fastcgi_param  MAGE_RUN_TYPE store;

        ...
    }

... ## Similar configuration blocks for other stores

}

The rewrite in the listen 80 block strips the store code from the URL and thus presents Magento with the correct REQUEST_URI. The store code is set based on the port that handles the request (which itself is determined by the store code in the URL). The store code is then rewritten into the URL when Magento does it's index.php redirection magic (for example, the base URL for the Dutch store is specified as http://www.example.com/nl/, so a request for /product/ is rewritten to http://www.example.com/nl/product/ if the Dutch store code is set).

Everything works reasonably well. The store code is set correctly when navigating to the subfolder URLs, none of the links on the page appear broken, and static files are served from the root of the domain.

Something about this setup makes me itchy, though. I know there's not a huge overhead for the proxy_pass, and with static files being served from port 80 there should only be one extra internal connection per pageload, but I'm worried that I'm missing something obvious (or worse, something non-obvious) that makes this a bad idea. Can anyone tell me why I shouldn't be routing requests this way, or make suggestions to improve performance?

One alternative option (suggested in another Stack Overflow thread) is to set up a nest of symlinks in /var/www/magento (e.g. ln -s /var/www/magento/ /var/www/magento/nl/en/). With the number of stores involved, that has the potential to get messy. (I also couldn't get it to work in a reasonable amount of time!)

Community
  • 1
  • 1
bclune
  • 129
  • 6
  • I've been trying to set up this kind of multistore structure for over a week and you're the first person I've found with the same use case. I'm amazed that this isn't documented better. – siliconrockstar Aug 21 '18 at 19:46

1 Answers1

0

Why all this separation into virtual hosts? Just set a variable based on the URL and then set the MAGE_RUN_CODE to the value of that variable.

  • Part of the issue is that Magento needs that rewrite to strip the store code from the base URL, otherwise it will look for `/var/www/magento/nl/en/index.php` regardless of store code. (Again, the symlinks could work here but that seems like a total hack.) Perhaps setting the REQUEST_URI parameter correctly would also work. Alternatively, I could duplicate the rewrite in _every_ location block, but the configs might get out of hand pretty quickly. – bclune May 08 '13 at 13:39
  • 1
    @bclune I'll try to reproduce this when I get home. Can you list your system - config - Web? The default view base_URL and value of the setting "redirect to base URL"? –  May 10 '13 at 16:48