Magento的。 Nginx的。 多店。 如何将“index.php”添加到网址

I downloaded magento with 3 stores and trying to install it on localhost.

  • Ubuntu 14
  • Nginx
  • Mysql
  • php-fpm

    /app
    /downloader
    /includes
    ...
    /store1/index.php
    /store2/index.php
    

Inside my store1/index.php I put $mageRunCode = 'my_store_code'

When I open url http://mysite.local.dev/store1/ everything is opening properly but all links don't contain 'index.php', without it all urls don't opening.

Returns 404:

http://mysite.local.dev/store1/some-cms-page.html

Opening well:

http://mysite.local.dev/store1/index.php/some-cms-page.html

Could you please tell me how to make url rewrite to add 'index.php' to my urls, or please advice another more clear solution.

Thanks in advance

server {

    listen 80;
    server_name local.dev *.local.dev;
    root /var/www/local.dev/www/$subdomain;
    set $subdomain "";
    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
    }
    if ($host ~* ^www.local.dev$) {
        set $subdomain "";
    }

    location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }

    location  /. { ## Disable .htaccess and other hidden files
        return 404;
    }

    location /api {
        rewrite ^/api/rest /api.php?type=rest last;
    }

    location @handler { ## Magento uses a common front handler
        rewrite / /index.php;
    }

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
        rewrite ^(.*.php)/ $1 last;
    }

    location ~ .php$ { ## Execute PHP scripts
        if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

        expires        off; ## Do not cache dynamic content
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 600; 
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

}

According to Emi`s solution.

If I clearly understand you - here is updated config:

server {

    listen 80;
    server_name local.dev *.local.dev;
    root /var/www/local.dev/www/$subdomain;
    set $subdomain "";
    set $magento_run_code "";
    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code $1;
    }
    if ($host ~* ^www.local.dev$) {
        set $subdomain "";
        set $magento_run_code "";
    }

    location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }

    location  /. { ## Disable .htaccess and other hidden files
        return 404;
    }

    location /api {
        rewrite ^/api/rest /api.php?type=rest last;
    }

    location @handler { ## Magento uses a common front handler
        rewrite / /index.php;
    }

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
        rewrite ^(.*.php)/ $1 last;
    }

    location ~ .php$ { ## Execute PHP scripts
        if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

        expires        off; ## Do not cache dynamic content
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 600; 
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE $magento_run_code; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

}

In my core_store table i have following (names are replaced):

default - sitename
default_store1 - store1
default_store2 - store2

unsecure urls are:

http://sitename.local.dev/
http://sitename.local.dev/store1/
http://sitename.local.dev/store2/

when I'm trying to open sitename.local.dev its trying to open with $mage_run_code = sitename. That because I'm getting 404. I think the expected value should be default.

What should be the url for store1? sitename.local.dev/store1 ?

upd.1 I understood your idea.

When we had

    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code $1;
    }

magento started executing with $1 which equals value in 3rd domain lvl. In my case http://sitename.local.dev - it is sitename.

I changed it to

    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code default;
    }

now it works well for first store.

Lets apply your solution for store1

According to this solution my current question is:

directory: {magento_root}/store1/*

mage_run_code: default_store1

url: http://store1.local.dev or http://sitename.local.dev/store1/ ??????

nginx config: as far as I can see it will depends on url. which url and which config should I use?

If you want to run Magento with 3 different stores, why don't you use the default settings? Default meaning, you set some variables in your nginx configuration, depending on your host, in your case you can use something just like $subdomain:

set $magento_run_code ""; #default value
if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
    set $subdomain $1;
    set $magento_run_code $1;
}
if ($host ~* ^www.local.dev$) {
    set $subdomain "";
    set $magento_run_code "";
}

Then under location you would have to set:

fastcgi_param  MAGE_RUN_CODE $magento_run_code;

The values must be defined from your admin (System > Manage Stores), otherwise Magento won't be able to load that store.

Also using the index.php can be tweaked from the admin in System > Configuration > GENERAL Web > Search Engines Optimization

For more information on your loaded store code, check these lines from index.php:

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';