# Map FastCGI Ports Dynamically

**URL:** <https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125>\
**Category:** NGINX\
**Created:** [February 22, 2012, 3:58am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125 "2012-02-22T03:58:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 22, 2012, 3:58am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/1 "2012-02-22T03:58:45Z")

</div>

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](http://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](http://php1.local.mydomain.com):$user\_port;

Posted at Nginx Forum:

> **[Map FastCGI Ports Dynamically](https://forum.nginx.org/read.php?2,222820,222820#msg-222820)**
>
> I have a config file that handles wildcard subdomains like: server\_name ~^(?\<user\>.+)\\.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...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 22, 2012, 9:06am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/2 "2012-02-22T09:06:43Z")

</div>

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](http://php1.local.mydomain.com):$user\_port;

Do you know about the “map” directive?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 22, 2012, 10:32am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/3 "2012-02-22T10:32:20Z")

</div>

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:

> **[Re: Map FastCGI Ports Dynamically](https://forum.nginx.org/read.php?2,222820,222836#msg-222836)**
>
> I have a config file that handles wildcard subdomains like: server\_name ~^(?\<user\>.+)\\.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...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 22, 2012, 11:19am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/4 "2012-02-22T11:19:22Z")

</div>

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?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 23, 2012, 3:19pm UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/5 "2012-02-23T15:19:28Z")

</div>

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:

> **[Re: Map FastCGI Ports Dynamically](https://forum.nginx.org/read.php?2,222820,222874#msg-222874)**
>
> I have a config file that handles wildcard subdomains like: server\_name ~^(?\<user\>.+)\\.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...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 22, 2012, 6:56pm UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/6 "2012-02-22T18:56:49Z")

</div>

Volodymyr,

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

Posted at Nginx Forum:

> **[Re: Map FastCGI Ports Dynamically](https://forum.nginx.org/read.php?2,222820,222863#msg-222863)**
>
> I have a config file that handles wildcard subdomains like: server\_name ~^(?\<user\>.+)\\.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...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 23, 2012, 4:27pm UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/7 "2012-02-23T16:27:53Z")

</div>

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:

> **[Re: Map FastCGI Ports Dynamically](https://forum.nginx.org/read.php?2,222820,222881#msg-222881)**
>
> I have a config file that handles wildcard subdomains like: server\_name ~^(?\<user\>.+)\\.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...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 23, 2012, 4:09pm UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/8 "2012-02-23T16:09:43Z")

</div>

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.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 23, 2012, 6:56pm UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/9 "2012-02-23T18:56:28Z")

</div>

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: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 23, 2012, 7:31pm UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/10 "2012-02-23T19:31:25Z")

</div>

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](http://forum.nginx.org/read.php?2,222820,222887#msg-222887)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 24, 2012, 3:58am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/11 "2012-02-24T03:58:00Z")

</div>

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

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#server)**
>
> This directive appeared in version 1.11.8.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [February 24, 2012, 4:54am UTC](https://www.ruby-forum.com/t/map-fastcgi-ports-dynamically/217125/12 "2012-02-24T04:54:22Z")

</div>

Volodymyr,

Thanks for the reply.

map $http\_host $user\_fastcgi\_port {  
default 9000;  
[justin.my.domain.com](http://justin.my.domain.com) 9001;  
[simon.my.domain.com](http://simon.my.domain.com) 9002;  
}

server\_name ~^(?.+).my.domain.com$;

fastcgi\_pass [php1.local.domain.com](http://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:

> **[Re: Map FastCGI Ports Dynamically](https://forum.nginx.org/read.php?2,222820,222906#msg-222906)**
>
> I have a config file that handles wildcard subdomains like: server\_name ~^(?\<user\>.+)\\.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...
