Home > Software design >  How to change context path of spring-boot actuator?
How to change context path of spring-boot actuator?

Time:06-29

I created a docker container of my spring-boot application with actuator endpoint enabled. Assuming the container runs on port 8080, it is accessible at localhost:8080/actuator, which exposes the endpoints as follows:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        ...
}

Problem: I'm behind an apache2 proxy, which redirects as follows:

ProxyPass https://myserver.com/api http://localhost:8080
ProxyPassReverse https://myserver.com/api http://localhost:8080

Now if I go to https://myserver.com/api/actuator, I can see the endpoints, but the rewritten context path is missing here: http://myserver/actuator/health

Question: how can I force Spring to build the management endpoint paths with an additional context-path /api?

My desired actuator endpoint output would be:

http://myserver/api/actuator/health

{
    "_links": {
        "self": {
            "href": "http://myserver/api/actuator",
            "templated": false
        },
        "health": {
            "href": "http://myserver/api/actuator/health",
            "templated": false
        },
        ... should be applied to all endpoints
}

Is that possible?

CodePudding user response:

Actually this is nothing that can be solved in spring directly, but with X-Forwarded-Prefix in the proxy in front:

apache2 config:

<VirtualHost *:443>
    <Location "/api">
        ProxyPass http://localhost:8080
        ProxyPassReverse http://localhost:8080
        
        RequestHeader set X-Forwarded-Prefix "/api"
    </Location>
</VirtualHost>

application.properties: server.forward-headers-strategy=framework

CodePudding user response:

You can see it here: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.monitoring

If you want to map endpoints to a different path, you can use the management.endpoints.web.path-mapping property.

The following example remaps /actuator/health to /api/actuator:

PropertiesYaml
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=api/actuator
  • Related