Magento and nginx - multiwebsite configuration

2019-09-09 23:43发布

问题:

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

回答1:

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.

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

  • 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…

  • 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

set

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

combine include and set

  • You should probably combine the above ideas to fit your exact need, considering the plans for future growth.

How do I use set conditionally?

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

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.



回答2:

It depends on the Distro you are using, in Ubuntu, /etc/nginx/nginx.conf file add this entry include /etc/nginx/sites-enabled/*; inside http{} section

Then create your individual websites under include /etc/nginx/sites-enabled/

Hope that helps.



标签: magento nginx