Json config strip_path_prefix not working

1. The problem I’m having:

Im trying to strip the path ‘/proxy/’ from a url eg: example.app/proxy/something
So that my backend sees the url as: example.app/something

My caddy config.json file below is working fine serving my app, but the path ‘proxy’ is not being stripped (I’ve tested using both postman and web browser) - the backend is still seeing ‘/proxy/’ in the URL.

2. Error messages and/or full log output:

No errors

3. Caddy version:

v2.7.6

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 22.04, systemd

b. Command:

started automatically by systemd

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/config.json
ExecReload=/usr/bin/caddy reload --config /etc/caddy/config.json --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

{
    "apps": {
        "http": {
            "servers": {
                "srv0": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                        {
                            "handle": [
                                {
                                    "handler": "headers",
                                    "response": {
                                        "deferred": false,
                                        "set": {
                                            "Content-Security-Policy": [
                                                "default-src 'self' example.b-cdn.net example2.b-cdn.net fonts.gstatic.com blob: data: 'unsafe-inline';"
                                            ],
                                            "Server": [
                                                "{system.hostname}"
                                            ],
                                            "Strict-Transport-Security": [
                                                "max-age=63072000; includeSubDomains; preload"
                                            ],
                                            "X-Content-Type-Options": [
                                                "nosniff"
                                            ]
                                        }
                                    }
                                },
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "subroute",
                                                    "routes": [
                                                        {
                                                            "handle": [
                                                                {
                                                                    "handler": "rewrite",
                                                                    "strip_path_prefix": "/proxy/"
                                                                }
                                                            ]
                                                        },
                                                        {
                                                            "handle": [
                                                                {
                                                                    "handler": "vars",
                                                                    "root": "/var/www/example/app/api/v1/public"
                                                                },
                                                                {
                                                                    "encodings": {
                                                                        "gzip": {}
                                                                    },
                                                                    "handler": "encode"
                                                                }
                                                            ]
                                                        },
                                                        {
                                                            "handle": [
                                                                {
                                                                    "handler": "static_response",
                                                                    "headers": {
                                                                        "Location": [
                                                                            "{http.request.uri.path}/"
                                                                        ]
                                                                    },
                                                                    "status_code": 308
                                                                }
                                                            ],
                                                            "match": [
                                                                {
                                                                    "file": {
                                                                        "try_files": [
                                                                            "{http.request.uri.path}/index.php"
                                                                        ]
                                                                    },
                                                                    "not": [
                                                                        {
                                                                            "path": [
                                                                                "*/"
                                                                            ]
                                                                        }
                                                                    ]
                                                                }
                                                            ]
                                                        },
                                                        {
                                                            "handle": [
                                                                {
                                                                    "handler": "rewrite",
                                                                    "uri": "{http.matchers.file.relative}"
                                                                }
                                                            ],
                                                            "match": [
                                                                {
                                                                    "file": {
                                                                        "split_path": [
                                                                            ".php"
                                                                        ],
                                                                        "try_files": [
                                                                            "{http.request.uri.path}",
                                                                            "{http.request.uri.path}/index.php",
                                                                            "index.php"
                                                                        ]
                                                                    }
                                                                }
                                                            ]
                                                        },
                                                        {
                                                            "handle": [
                                                                {
                                                                    "handler": "reverse_proxy",
                                                                    "transport": {
                                                                        "protocol": "fastcgi",
                                                                        "split_path": [
                                                                            ".php"
                                                                        ]
                                                                    },
                                                                    "upstreams": [
                                                                        {
                                                                            "dial": "unix//run/php/php8.3-fpm-example.sock"
                                                                        }
                                                                    ]
                                                                }
                                                            ],
                                                            "match": [
                                                                {
                                                                    "path": [
                                                                        "*.php"
                                                                    ]
                                                                }
                                                            ]
                                                        }
                                                    ]
                                                }
                                            ],
                                            "match": [
                                                {
                                                    "path": [
                                                        "/proxy/*"
                                                    ]
                                                }
                                            ]
                                        },
                                        {
                                            "handle": [
                                                {
                                                    "handler": "vars",
                                                    "root": "/var/www/example/app/fe"
                                                }
                                            ]
                                        },
                                        {
                                            "handle": [
                                                {
                                                    "handler": "rewrite",
                                                    "uri": "{http.matchers.file.relative}"
                                                }
                                            ],
                                            "match": [
                                                {
                                                    "file": {
                                                        "try_files": [
                                                            "{http.request.uri.path}",
                                                            "{http.request.uri.path}/",
                                                            "/index.html"
                                                        ]
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            "handle": [
                                                {
                                                    "handler": "file_server"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "match": [
                                {
                                    "host": [
                                        "example.app"
                                    ]
                                }
                            ],
                            "terminal": true
                        }
                    ]
                }
            }
        }
    }
}

Can anyone see what I’m doing wrong in my caddy config?

Enable debug level logging, and show your logs. It’ll log about the rewrites, and the proxy will show the URI it sends upstream.

You probably want to strip /proxy by the way, not /proxy/, because valid request paths must start with a /.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.