1

My server answers to the following virtual subdirectory paths:

www.domain.com/us www.domain.com/ca www.domain.com/fr-ca www.domain.com/spa

Each of these is an alias for www.domain.com.

If I attempt to access www.domain.com/some/virtual/path, it is correctly handed off to my index.php and processed by PHP-FPM.

If I attempt to access www.domain.com/us/some/virtual/path, it is correctly handed off to my index.php and processed by PHP-FPM

However, if I try to call www.domain.com/us/file.php, NGINX attempts to serve the file as a download. But without the virtual path, it is handled appropriately by PHP-FOM.

My virtual subdirectory paths are managed by this section in my NGINX config:

    ####
    # Catch virtual locations: /us, /ca, /fr-ca, /spa
    ####
    location ~ ^\/(?:(?<currentSite>us|ca|fr-ca|spa)(?:\/|$))(?<realPath>.*) {
        try_files /$realPath /$realPath/;
        break;
    }

.

server {
    listen       443 ssl;
    listen       80;
    listen   [::]:80 default ipv6only=on;
    server_name  localhost;

    ssl_certificate      /certs/cert.pem;
    ssl_certificate_key  /certs/cert.key;

    root   ${LANDO_WEBROOT};
    index index.php index.html index.htm;

    ######
    # CloudFlare limit for headers is 8K - development should simulate this

    large_client_header_buffers 4 8k;

    ####
    # Catch virtual locations: /us, /ca, /fr-ca, /spa
    ####
    location ~ ^\/(?:(?<currentSite>us|ca|fr-ca|spa)(?:\/|$))(?<realPath>.*) {
        try_files /$realPath /$realPath/;
        break;
    }

    ####
    # Manage the REAL locations
    ####
    location /js {
        index index.html
        expires 100d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
        break;
    }

    location /media {
        index index.html
        expires 100d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
        break;
    }

    location /skin {
        index index.html
        expires 100d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
        break;
    }

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

    location / {
        try_files $uri $uri/ @handler;
    }

    location ~ \.php$ {
        # Try to load the file if requested, if not found, rewrite to @missing
        # This should pass all requests through index.php

        try_files $uri =404;

        fastcgi_param MAGE_IS_DEVELOPER_MODE true;
        fastcgi_buffers 256 500k; #Allow a greater amount of data to be included in cookies
        fastcgi_buffer_size 1000k;  #Allow a greater amount of data to be included in cookies

        fastcgi_pass fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
        include fastcgi_params;
    }
}
1

You can use a rewrite...last to remove the language prefix from the URI, so that it can be processed correctly by the remaining locations in your configuration. In particular, .php URIs need to be processed by the location ~ \.php$ block.

For example:

location ~ ^/(?:(?<currentSite>us|ca|fr-ca|spa)(?:/|$))(?<realPath>.*) {
    rewrite ^ /$realPath last;
}

Or more simply:

rewrite ^/(us|ca|fr-ca|spa)(?:/(.*))? /$1 last;

See this document for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.