本文介绍了Magento使用Nginx proxy_pass存储在子文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用单独的商店视图设置Magento安装以进行本地化.但是,我不确定我目前在Nginx中实现URL处理的方式是否是最干净的解决方案.

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.

URL结构为:

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

,依此类推.不管URL结构如何(http://www.example.com/nl/en/index.phphttp://www.example.com/index.php都与/var/www/magento/index.php对应),将从/var/www/magento/提供每个商店视图的PHP代码.

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

我们目前正在寻找16家独立的Magento商店,最终可能会将其扩大到30家以上.许多商店都关注Magento/PHP的性能,但现在我更担心Nginx.

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.

当前端口80处理所有传入连接,并且基于本地化代码,使用proxy_pass将它们重定向到localhost上的单独Nginx服务器.然后,该服务器设置MAGE_RUN_CODE变量以将商店代码指定为Magento:

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

}

listen 80块中的重写会从URL中剥离商店代码,从而为Magento提供正确的REQUEST_URI.商店代码是根据处理请求的端口设置的(端口本身由URL中的商店代码确定).当Magento执行index.php重定向魔术时,商店代码随后被重写为URL(例如,荷兰商店的基本URL被指定为http://www.example.com/nl/,因此如果/product/的请求被重写为http://www.example.com/nl/product/),荷兰商店代码已设置.)

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

一切正常.导航到子文件夹URL时,商店代码已正确设置,页面上的所有链接均未断开,并且从域的根目录提供了静态文件.

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.

不过,有关此设置的某些信息使我发痒.我知道proxy_pass并没有太大的开销,并且从端口80提供静态文件时,每个页面加载应该只有一个额外的内部连接,但是我担心我遗漏了一些明显的东西(或更糟糕的是,显而易见),因此这是个坏主意.谁能告诉我为什么我不应该以这种方式路由请求或提出改善性能的建议?

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?

一个替代选项(建议在另一个Stack Overflow线程中)用于在/var/www/magento(例如ln -s /var/www/magento/ /var/www/magento/nl/en/)中建立符号链接的嵌套.由于涉及的商店数量众多,因此有可能变得凌乱. (我也无法在合理的时间内使它正常工作!)

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

推荐答案

为什么将所有这些分离成虚拟主机?只需根据URL设置一个变量,然后将MAGE_RUN_CODE设置为该变量的值即可.

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.

这篇关于Magento使用Nginx proxy_pass存储在子文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:08