Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andreyka26-git/3af4c4b4f39dbf3df462a9df9003dfc7 to your computer and use it in GitHub Desktop.
Save andreyka26-git/3af4c4b4f39dbf3df462a9df9003dfc7 to your computer and use it in GitHub Desktop.
nginx dynamic proxy_pass with routes (explaining in the source code)
# this config resolves different containers depending on route path, not on ips/domains
# on top of that it uses dynamic resolving, meaning that if some docker containers are not alive
# nginx will still work. It is achieved by using variables which makes nginx resolve host on runtime and not on start.
# frontend dev env
server {
# nginx container port
listen 3001;
server_name localhost;
location /auth/back/ {
# this needed to resolve host by docker dns, othervise 'set $upstream will' not work
resolver 127.0.0.11 valid=10s;
# https://stackoverflow.com/questions/71204607/why-does-a-variable-not-work-in-nginx-proxy-pass
set $upstream <backend-docker-host>;
rewrite /auth/back/(.*) /$1 break;
proxy_pass http://$upstream/authorization/$1$is_args$args;
# this is need to set proper host header for google auth redirect
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto https;
}
location /auth/front/ {
# this needed to resolve host by docker dns, othervise 'set $upstream will' not work
resolver 127.0.0.11 valid=10s;
# https://stackoverflow.com/questions/71204607/why-does-a-variable-not-work-in-nginx-proxy-pass
set $upstream <frontend-docker-host>:3000;
rewrite /auth/front/(.*) /$1 break;
proxy_pass http://$upstream/authorization/$1$is_args$args;
# this is need to set proper host header for google auth redirect
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto https;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment