Nginx – Multiple magento sites running under different users in nginx

magentonginx

I have written a script that copies magento files and database from our production server and tries to configure it on a subdomain on our testing server.

Each testing site has:
– unique subdomain
– running under unique user specified in php-fpm pool

Here is the php-fpm pool config:

[test1]
user = test1
group = test1
listen = /run/php/php7.0-test1-fpm.sock
listen.owner = www-data
listen.group = www-data

So I would set up subsequent sites under different users like so:

[test2]
user = test2
group = test2
listen = /run/php/php7.0-test2-fpm.sock
listen.owner = www-data
listen.group = www-data

The problem comes when I try to duplicate the magento nginx server block (abbreviated version pasted below):

 upstream fastcgi_backend {
     server  unix:/run/php-fpm/php-test1-fpm.sock;
 }

 server {

     listen 80;
     server_name test1.magento-dev.com;
     set $MAGE_ROOT /usr/share/nginx/html/test1;
     include /usr/share/nginx/html/test1/nginx.conf.sample;
 }

If I duplicate the config like so:

 upstream fastcgi_backend {
     server  unix:/run/php-fpm/php-test2-fpm.sock;
 }

 server {

     listen 80;
     server_name test2.magento-dev.com;
     set $MAGE_ROOT /usr/share/nginx/html/test2;
     include /usr/share/nginx/html/test2/nginx.conf.sample;
 }

I get error because the fastcgi_backend upsteam is already defined. I read the nginx documentation for upstream and it says it is a pool of servers but I don't really understand what is going on here to specify upstream like this to handoff php requests.

What am I doing wrong? How should I setup multiple magento sites on subdomains running under different users?

How I fixed it:

I renamed the upstream but I didn't realise that the upstream name is used in proxy_pass. You have to edit the proxy_pass in nginx.conf.sample in magento root.

Best Answer

Here's how I set up multiple pools using Nginx and PHP 5.6. I don't use paths, I use sockets. I've slightly edited my files to make them more generic, so if anything doesn't match assume it's a typo.

/etc/php-fpm-5.6.d/pool1

[pool1]
listen = 127.0.0.1:9000

/etc/php-fpm-5.6.d/pool2

[pool2]
listen = 127.0.0.1:9001

/etc/nginx/upstreams.conf

upstream php56-pool1 {
    server 127.0.0.1:9000;
}

upstream php56-pool2 {
    server 127.0.0.1:9001;
}

Here are relevant parts of my Nginx location blocks.

/etc/nginx/site1.conf

location ~ \.php$ {
    fastcgi_pass   php56-pool1;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
}

/etc/nginx/site2.conf

location ~ php$ {
    fastcgi_pass php56-pool2;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}