【问题标题】:Magento and nginx - multiwebsite configurationMagento 和 nginx - 多网站配置
【发布时间】:2013-12-24 05:47:58
【问题描述】:

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

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; 
}

【问题讨论】:

    标签: magento nginx


    【解决方案1】:

    我考虑过为您重写整个配置文件,但是,如果您像这样简单地从网络上复制粘贴某些内容,那将是一个坏主意,而且还可能违反 StackOverflow 答案格式的精神。

    所以,让我给你一些很好的指导,告诉你如何在一般意义上自己完成你需要的事情。

    此外,一般而言,就 nginx 和性能而言,拥有多个几乎相同的 server 指令并不是一个坏主意——它们只是从管理的角度来看是不好的,在这方面不是很清楚有人在查看服务器之间的区别。

    include

    • 您可以使用include 指令将每个服务器分隔到一个单独的文件中。这样,任何查看您的设置的人都可以轻松地使用diff 来清楚地看到服务器之间的区别,您还可以对其中一个服务器文件进行更改,然后使用patch 将所述更改应用于其他人,这也让我们找到了根本原因……

    • 您可以使用include 将每个server 的所有相同部分放在一个给定的文件中,其中每个server 将非常简单,并且只会列出配置之间的实际差异

    set

    • 您可以使用变量来分隔传递给某些指令的值。请注意,直接在 if 中使用各种指令通常不起作用,但使用中间变量可以实现同样的效果。

    结合includeset

    • 考虑到未来的发展计划,您可能应该结合上述想法来满足您的确切需求。

    如何有条件地使用set

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

    set $mage_rc "default";
    if ($server_name = test1.com) {
        set $mage_rc "test1";
    }
    
    location ~ \.php$ {
        …
        fastcgi_param  MAGE_RUN_CODE $mage_rc;
        …
    }
    

    那么,我从哪里开始呢?

    根据您提供的信息,我可能会将服务器分开(即仍然使用单独的 server 指令),在每个 serverinclude 中定义一些局部变量,这两个服务器共有的配置,在上述通用配置中,将使用$mage_rc 之类的变量,这些变量应该在每个server 中定义。使用单独的servers,就不需要像上面那样使用if — 您只需在每个server 中分别定义每个变量,一次用于整个server 上下文。

    【讨论】:

      【解决方案2】:

      这取决于您使用的发行版,在 Ubuntu 中,/etc/nginx/nginx.conf 文件在 http{} 部分添加此条目 include /etc/nginx/sites-enabled/*;

      然后在 include /etc/nginx/sites-enabled/ 下创建您的个人网站

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2012-08-26
        • 2019-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-25
        • 2011-05-20
        • 1970-01-01
        相关资源
        最近更新 更多