Magento和Nginx-多网站配置 [英] Magento and nginx - multiwebsite configuration

查看:90
本文介绍了Magento和Nginx-多网站配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我直接在Magento多网站中对nginx使用以下配置.一切正常,但我不喜欢这种配置没有优化,因此非常庞大.如您所见,两个服务器配置几乎相同,只有server_name,ssl和fastcgi_param MAGE_RUN_CODE不同.如何加入2个服务器配置?如您所见,这里有很多重复项.

Directly now I use configuration below for nginx in my Magento multiwebsite. All works fine, but I don't like that this configuration is not optimized and as a result is very huge. As you can see 2 server configurations are nearly the same, only server_name, ssl and fastcgi_param MAGE_RUN_CODE are different. How can I join 2 server configurations? As you can see there are a lot of duplication here.

server {
    listen 80;
    server_name test.com test1.com;
    rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
}

server {
    listen 80;
## SSL directives might go here
    listen 443 ssl;
    ssl_certificate     /etc/nginx/ssl/test/cert.cer;
    ssl_certificate_key      /etc/nginx/ssl/private/test.key;
    server_name www.test.com test.com;
    root /var/www/test;

    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 @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   127.0.0.1:9000;
        fastcgi_param  HTTPS $fastcgi_https;
        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
    }

    error_page 403 /error-page.html; 
    error_page 502 /error-page.html; 
    error_page 503 /error-page.html; 
    error_page 504 /error-page.html; 
}

server {
    listen 80;
## SSL directives might go here
    listen 443 ssl;

    server_name test1.com www.test1.com;
    root /var/www/test;

    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 @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   127.0.0.1:9000;
        fastcgi_param  HTTPS $fastcgi_https;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE test1; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

    error_page 403 /error-page.html; 
    error_page 502 /error-page.html; 
    error_page 503 /error-page.html; 
    error_page 504 /error-page.html; 
}

推荐答案

我曾考虑过为您重写整个配置文件,但是,对您来说,仅是简单地从网络中复制粘贴内容,这对您来说是个坏主意,并且可能还会违反StackOverflow答案格式的精神.

I thought about rewriting the whole config file for you, however, it would be a bad idea for you to simply copy-paste something from the web like that, and would also probably violate the spirit of StackOverflow answer format.

所以,让我为您提供一些很好的指导,以概括的方式说明如何自己完成所需的事情.

So, let me give you some good pointers of how you can accomplish what you need by yourself, in a general sense.

通常,就nginx和性能而言,拥有多个几乎相同的server指令不是一个坏主意—从管理的角度来看,它们只是不好的,在这种情况下,看着他们的服务器并不清楚服务器之间有什么区别.

Also, in general, and as far as nginx and performance are concerned, it's not a bad idea to have multiple nearly identical server directives — they are only bad from the administrative point of view, where it's not very clear to someone looking at them of what's the difference between the servers.

  • 您可以使用include指令将每个服务器分离为一个单独的文件.这样,任何查看您的设置的人都可以轻松地使用 diff 清楚地了解两者之间的区别服务器,也可以对其中一个服务器文件进行更改,然后使用 patch ,这也将我们带到了根源…

  • You can use the include directive to separate each server into a separate file. This way, anyone looking at your setup can easily use diff to clearly see what's the difference between the servers is, and you could also possibly make changes to one of the server files, and then apply said changes to others with patch, which also brings us to the root cause…

您可以使用include将每个server中的所有相同部分放在单个给定文件中,其中每个server都将成为准系统,并且仅列出配置之间的实际差异

You can use include to have all the identical parts from each server in a single given file, where each server will then be quite barebone, and will only list the actual differences between the configurations

  • 您可以使用变量来分隔传递给某些指令的值.请注意,直接在if中直接使用各种指令通常是行不通的,但是使用中间变量可以使您完成相同的操作.
  • You can use variables to separate out the values passed to some directives. Note that using all sorts of directives directly within if generally doesn't work, but using an intermediate variable lets you accomplish the same.
  • 考虑未来的增长计划,您应该结合以上想法来满足您的确切需求.

如果您希望将整个内容放入一个server中,则可以有条件地使用set:

If you wish to fit the whole thing within one server, you can use set conditionally:

set $mage_rc "default";
if ($server_name = test1.com) {
    set $mage_rc "test1";
}

location ~ \.php$ {
    …
    fastcgi_param  MAGE_RUN_CODE $mage_rc;
    …
}

那么,我应该从哪里开始?

使用您提供的信息,我可能会将服务器分开(即仍使用单独的server指令),在每个server中定义一些局部变量,并为两个服务器共用配置include,在上述通用配置中,将使用像$mage_rc这样的变量,该变量应在每个server中进行定义.如果使用单独的server,则无需像上面—一样使用if.您只需在每个server内分别定义每个变量,一次用于整个server上下文.

So, where do I start?

With the info you've provided, I would probably keep the servers separate (i.e. still use separate server directives), define some local variables in each server, and include the configuration common to both servers, where in the said common configuration would use the variables like $mage_rc, which should carry on from being defined within each server. With separate servers, there's no need to use if as above — you'd simply define each variable separately within each server, once for the whole server context.

这篇关于Magento和Nginx-多网站配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆