I’m have implemented Spring Cloud Gateway with Eureka discovery service, everything works fine but I have seen something that I don’t know how to deal with when I write the URL and if I don’t put a / at the end of the URL the gateway redirects to the app directly using its actual URL (registered in Eureka).
For example:
- https://example.com/bar redirect to the app URL (http://example.app.url.com:8010/bar/)
- https://example.com/bar/ works as expected (it maintain the actual gateway URL)
Is there a configuration to avoid the first situation?
My configuration is the following:
spring: application: name: gateway-service cloud: gateway: routes: - id: bar-service uri: lb://BAR-SERVICE/ predicates: - Path=/bar/** - id: other-service uri: lb://OTHER-SERVICE/ predicates: - Path=/OTHER/**
Additional information:
- I have a controller in every app that has ‘/’ as an entry point (home page)
- I can use java configuration instead if necessary
Any advice will be appreciated! Cheers!
Advertisement
Answer
You should use RewritePath in gateway configuration. The below is sample and hope it helpful to you.
spring: application: name: gateway-service cloud: gateway: routes: - id: bar-service uri: lb://BAR-SERVICE/ predicates: - Path=/bar/** filters: - RewritePath=/bar(?<segment>.*), /${segment}