I want my nginx docker to be able to proxy_pass dynamically if anyhow using one simple config..
I currently use this config for three apps, but I would like to make it work dynamically that I could also use it with /xyz_987 which automatically passes to http://xyz_987:3000:
server {
listen 3000;
server_name _;
location /user1/ {
proxy_pass http://user1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location /user2/ {
proxy_pass http://user2:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location /user3/ {
proxy_pass http://user3:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
I tried this but it always results in a 502 Bad Gateway:
server {
listen 3000;
server_name _;
location ~ ^/(?<username>[\w]+)/ {
proxy_pass http://$username:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
# Fallback for root path
location / {
return 404;
}
}