0

I have multiple node websockets that are available through a url matching the UNIX socket paths. Instead of duplicating the location directive, I would like to have something like a list of Urls that will be used as socket name. Using a regex does not work, because I need the path for the socket name.

Currently I'm using this location:

location /application1/
{
   proxy_pass http://unix:/var/run/nodejs/application1;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}

the goal is to have something like:

$list = [app1, app2, app3];           #list of possible names
location /$list/   #match all names in the list
{
   proxy_pass http://unix:/var/run/nodejs/$list_matched;   #use matched name
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}

Basically every URL in a list should be redirected to the socket with the same name.

Thanks in advance for any help :)

zuim
  • 180
  • 1
  • 7

2 Answers2

1
location ~* /(<regexp>)/ {
    proxy_pass http://unix:/var/run/nodejs/$1;   #use matched name
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
drookie
  • 8,625
  • 1
  • 19
  • 29
  • The `$1` was exactly what was missing. In combination with `location ~ /(app1|app2|app3)/` to match all hostnames it works perfectly! – zuim Jan 21 '17 at 21:41
0

You need to use a regular expression. Something like this should work, but you will have to work out the regular expression yourself. There are plenty of tutorials and regular expression experimentation sites.

location ~ (app1|app2|app3)$ {
Tim
  • 31,888
  • 7
  • 52
  • 78