Home > Blockchain >  Nginx Location Regex for keycloak
Nginx Location Regex for keycloak

Time:01-08

I have a keycloak, react web and a spring boot app are living behind nginx. The frontend (react) and the backend (spring boot) are calling the keycloak service over the nginx proxy at the flowing urls:

  • realms/.../..etc
  • admin/.../..etc
  • js/.../.../..etc
  • resources/.../...etc

All those urls are reverse proxing to:

Http://keylock:8180

I don't want to implements multiple locations in nginx for all those urls!

I am thinking about url rewriting to

  • auth/realms/...
  • auth/dmin/.. ...

Or another clean solution with regex, but i don't know how.

CodePudding user response:

You can use the rewrite module for this.

location /auth {
    rewrite ^/auth(/|$)(.*) /$2/ break;
    proxy_pass    http://keylock:8180;
}

In fact, with this method, I get the second part of the url and proxy it.

For example, when request send to /auth/realms/... the url rewrite to /realms/... and send it to keycloak http://keylock:8180/realms/...

CodePudding user response:

this worked for me

    location  ~ ^/(realms|js|resources|admin)/ {
    proxy_set_header Referer $http_referer;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header Host $http_host;
    proxy_pass http://keycloak;
    proxy_redirect off;
}
  • Related