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!)