Map FastCGI Ports Dynamically

I have a config file that handles wildcard subdomains like:

server_name ~^(?.+).mydomain.com$;

Then the variable $user contains the subdomain. The problem, is that I
also need to dynamically set the port that fastcgi passes PHP work too
since each user has their own PHP-FPM pool.

fastcgi_pass php1.local.mydomain.com:XXXX;

Is there a way to setup a hashmap of something like:

user fastcgi-port


bob 9001
john 9002
kelly 9003

So that nginx can do some magic like:

set $user_port = map($user);

fastcgi_pass php1.local.mydomain.com:$user_port;

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222820#msg-222820

On Wednesday 22 February 2012 06:58:09 justin wrote:

Is there a way to setup a hashmap of something like:

fastcgi_pass php1.local.mydomain.com:$user_port;

Do you know about the “map” directive?

http://wiki.nginx.org/HttpMapModule

wbr, Valentin V. Bartenev

Valentin,

Yeah, but failing to put the pieces together. How would I use map to
build a sort of dynamic table that stored username, port combos?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222836#msg-222836

On Wednesday 22 February 2012 13:31:41 justin wrote:

Valentin,

Yeah, but failing to put the pieces together. How would I use map to
build a sort of dynamic table that stored username, port combos?

server_name ~^(?.+).mydomain.com$;

user fastcgi-port


bob 9001
john 9002
kelly 9003

map $user $fastcgi_port {
default ‘’;
bob 9001;
john 9002;
kelly 9003;
}

Did you read documentation?
http://wiki.nginx.org/HttpMapModule

wbr, Valentin V. Bartenev

Valentin:

Here is a snippet of the config:

server {
listen 80;

map $user $user_fastcgi_port {
default 9000;
bob 9001;
john 9002;
kelly 9003;
}

server_name $user.my.domain.com$;

root /srv/www/users/$user/wp;

index index.php;

access_log /var/log/nginx/users/$user.access.log;
error_log /var/log/nginx/error.log;

include /etc/nginx/excludes.conf;
include /etc/nginx/wordpress.conf;
include /etc/nginx/expires.conf;
}

I am getting the following error though:

nginx: [emerg] “map” directive is not allowed here in
/etc/nginx/conf.d/core.conf:4
nginx: configuration file /etc/nginx/nginx.conf test failed

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222874#msg-222874

Volodymyr,

Thanks, but the fastscgi backend is actually a different server, so have
to use TCP.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222863#msg-222863

Valentin,

Would you have a bit of time to assist personally, would only take you
less than an hour and we can paypal for your time?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222881#msg-222881

On Thursday 23 February 2012 01:16:33 justin wrote:

john 9002;

error_log /var/log/nginx/error.log;
nginx: configuration file /etc/nginx/nginx.conf test failed

Yeap, if you look at the documentation, you will see:

map

  • syntax: map $var1 $var2 { … }
  • default: none
  • context: http

Please, notice that “context: http”, therefore you must define “map”
block in
the “http” context, not the “server” one. And error log message is
trying to
give a hint.

http://wiki.nginx.org/HttpMapModule

wbr, Valentin V. Bartenev

On Thursday 23 February 2012 02:24:55 justin wrote:

Valentin,

Would you have a bit of time to assist personally, would only take you
less than an hour and we can paypal for your time?

You could make an inquiry by email: [email protected]

wbr, Valentin V. Bartenev

Valentin,

Ok, well I am almost there. The problem is since we need to define the
map block outside of server, the $user variable is not defined yet.
I.E.

http {

$user is not defined yet, it comes from inside server {}

map $user $user_fastcgi_port {
default 9000;
justin 9001;
simon 9002;
}
}

server {

Set’s $user

server_name ~^(?.+).my.domain.com$;
}

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222887#msg-222887

On Thursday 23 February 2012 05:28:04 justin wrote:

justin 9001;
simon 9002;

}
}

server {

Set’s $user

server_name ~^(?.+).my.domain.com$;
}

Did you try, or it’s just your guess? There’s no “variable scope”
concept in
nginx configuration. And it mostly has declarative nature, therefore,
the order
of most directives is not important, unless it is specifically
documented.

btw, your example config also wrong. If look at the documentation, you
will see:

server
syntax: server { … }
default: —
context: http

http://nginx.org/en/docs/http/ngx_http_core_module.html#server

wbr, Valentin V. Bartenev

Volodymyr,

Thanks for the reply.

map $http_host $user_fastcgi_port {
default 9000;
justin.my.domain.com 9001;
simon.my.domain.com 9002;
}

server_name ~^(?.+).my.domain.com$;

fastcgi_pass php1.local.domain.com:$user_fastcgi_port;

Getting 502 Bad Gateway. How can I debug and see what the value of
$user_fastcgi_port is being set as?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222820,222906#msg-222906