Pass environment variable to php script through php-fpm

231 阅读1分钟

Today I met a weird problem, domain A and domain B pointed to the same project, but they had different config values as to the same config item. After asking for help from my colleague, he told me that the two domains had different php-fpm conf files, all of the config values were passed through their php-fpm conf files respectively. Here is the framework of the project,

  1. the nginx conf: domainA
        location  / {
                set $script_uri "";
                if ( $request_uri ~* "([^?]*)?" ) {
                        set $script_uri $1;
                }
                fastcgi_pass   unix:/dev/shm/php-fpm-1.sock;
                fastcgi_param  SCRIPT_URL   $script_uri;
                include comm_fastcgi_params;
        }

domainB

location  / {
                set $script_uri "";
                if ( $request_uri ~* "([^?]*)?" ) {
                        set $script_uri $1;
                }
                fastcgi_pass   unix:/dev/shm/php-fpm-3.sock;
                fastcgi_param  SCRIPT_URL   $script_uri;
                include comm_fastcgi_params;
        }

Notice that the fastcgi_pass has different values.
2) php-fpm www.conf

[xxx]
user = www
group = www
listen = /dev/shm/php-fpm-1.sock
listen.allowed_clients = 127.0.0.1
listen.mode = 0666
pm = static
pm.max_children = 1
pm.max_requests = 1500
request_slowlog_timeout = 20
request_terminate_timeout = 30
catch_workers_output = no
security.limit_extensions = ""
include = /etc/php-fpm.d/env-1.conf
[yyy]
user = www
group = www
listen = /dev/shm/php-fpm-3.sock
listen.allowed_clients = 127.0.0.1
listen.mode = 0666
pm = static
pm.max_children = 1
pm.max_requests = 1500
request_slowlog_timeout = 20
request_terminate_timeout = 30
catch_workers_output = no
security.limit_extensions = ""
include = /etc/php-fpm.d/env-3.conf

So after starting php-fpm process, there will be thress processes total. Requests of domainA and domainB will be passed to xxx and yyy respectively. Here is env-1.conf and env-3.conf

env[MYSQL_M]='x'
env[MYSQL_R]='y'
env['MYSQL_M]='a'
env['MYSQL_R]='b'

Then in the project code, mysql address can be got through getenv('MYSQL_M') calling. In this way, these two projects use the same code but have different config values.
Compared to the traditional way, which usually config items in the project config dir, for example domainA.ini and domainB.ini, this way of configing items is something weird, but can be considered in the future.