我希望我的 nginx docker 能够使用一个简单的配置动态地进行 proxy_pass。
我目前在三个应用程序中使用了此配置,但我想使其动态工作,以便我也可以将它与 /xyz_987 一起使用,它会自动传递到 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;
}
}
我尝试过这个但总是导致 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;
}
}