0

I'm trying to use nginx to redirect to ports (running nodeJS apps) based on the domain prefix. So far, I can redirect

  • example.com:80 --> port 8502
  • 5555.example.com:80 --> port 5555
  • 6666.example.com:80 --> port 6666

Is there a way to do this kind of redirection without having to copy-paste this over and over?

server {
  listen 80;
  server_name 5555.example.com;
  location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_pass http://example.com:5555;
  }
}

I figured I should do this with regular expressions, so I tried the following, but without any success :

~^(?<theport>.+)\.example\.com$   #then changed proxy_pass to http://example.com:$theport

~^([0-9]+)\.example\.com$  #then changed proxy_pass to http://example.com:$1
  • You might be able to do it with a map and variables. – Tim Aug 06 '16 at 22:26
  • I'm new to nginx, could you elaborate a little please? :) – Bernard Saucier Aug 06 '16 at 22:28
  • you could probably do this programmatically with [Varnish](http://varnish-cache.org) (which allows you to compile your own c++ modules in) or our product [WinGate](http://www.wingate.com) which allows you to alter requests with lua or jscript. Disclaimer: I work for Qbik who make WinGate. – Adrien Aug 08 '16 at 10:41
  • @BernardSaucier did you solved this issue? I'm get in the same trouble... – Beto Neto Jul 11 '18 at 17:02
  • Take a look here https://serverfault.com/questions/920534/nginx-proxy-subdomains-to-other-addresses-and-ports – Beto Neto Jul 11 '18 at 20:04
  • I did figure it out : https://stackoverflow.com/questions/38808860/nginx-redirect-to-port-according-to-domain-prefix-dynamically/38810749#38810749 – Bernard Saucier Jul 16 '18 at 13:11

0 Answers0