springspring-bootspring-actuator

How to change context path of spring-boot actuator?


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?


Solution

  • 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