Nginx config for Magento subfolder multistore

magentonginxrewrite

I'm moving from subdomain to subfolder.

I have multiple subfolders for several countries. I changed the domain in the Magento administration. But the Nginx rewrite doesn't seem to work. I have 1 Magento installation on my server and want to use /uk and /us.

The page is loading but all pages are giving me a 404 error.

I've been spending many hours on Google but can't find a working configuration. Hope someone can give a clue.

This is my config:

server {
listen 443 ssl http2;   
root /home/domain_com/public_html/;
server_name www.domain.com/uk;
if ($http_host = cache.domain.com) {
rewrite  (.*)  https://www.domain.com$1 permanent;
}
if ($http_host = uk.domain.com) {
rewrite  (.*)  https://www.domain.com/uk$1 permanent;
}
location / {
index index.html index.php;
try_files $uri $uri/ @handler;
expires 30d;
}
location /downloader/ {
allow xx.xx.xx.xx;
deny all;
}
location ~ ^/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location @handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass domain;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_param MAGE_RUN_CODE ic_uk;
fastcgi_param MAGE_RUN_TYPE website;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_read_timeout 60;
include /etc/nginx/fastcgi_params;
    }
}

Best Answer

Your configuration has lots of problems. I don't know where you found it, but I would advise you not to go there again.

First:

location /. { return 404; }

It will match everything but the home page. It's not clear why you have this to begin with.

Second:

server_name www.domain.com/uk;

This is obviously not valid, as / cannot appear in host names. Change this to something appropriate.

Third:

location @handler { rewrite / /index.php; }

You send everything here which isn't a static resource from your try_files, but the rewrite only matches the URL path /. So again, you should expect to get 404s from everything except the home page. This can be trivially fixed by changing it to ^, which will match everything.

It's also not clear why you don't just pass directly to FastCGI from this location. The rest of them are redundant.


Those are just the obvious problems I see which will prevent the config from working at all. There are still plenty of other bad practices in here (such as improper use of if) which you should also clean up later.