V2: Reverse proxy strip part of path

1. My Caddy version (caddy version):

v2.0.0-rc.3 h1:z2H/QnaRscip6aZJxwTbghu3zhC88Vo8l/K57WUce4Q=

2. How I run Caddy:

a. System environment:

Ubuntu18.04

Caddy is run via systemd service unit.

d. My complete Caddyfile or JSON config:

{
    "apps": {
        "http": {
            "servers": {
                "srv0": {
                    "listen": [":443"],
                    "routes": [
                        {
                            "match": [
                                {
                                    "host": ["localhost", "10.0.2.15"]
                                }
                            ],
                            "handle": [
								{
									"handler": "subroute",
									"routes": [
										{
											"handle": [
												{
													"encodings": {
														"gzip": {}
													},
													"handler": "encode"
												}
											]
										},
										{
											"handle": [
												{
													"handler": "rewrite",
													"uri_substring": [
                                                        {
                                                            "find": "/test",
                                                            "replace": "",
                                                            "limit": 0
                                                        }
                                                    ]
												},
												{
													"handler": "reverse_proxy",
													"upstreams": [
														{
															"dial": "localhost:4325"
														}
													]
												}
											],
											"match": [
												{
													"path": [
														"/test*"
													]
												}
											]
										},
										{
											"handle": [
												{
													"handler": "reverse_proxy",
													"upstreams": [
														{
															"dial": "localhost:4321"
														}
													]
												}
											]
										}
									]
								}
							],
                            "terminal": true
                        }
                    ]
                }
            }
        }
    }
}

3. The problem I’m having:

I’d like to have two reverse proxies set up - one which forwards requests made without any path (i.e. requests to /) to localhost:4321 and one which forwards requests to /test* to localhost:4325. For the second proxy, I’d like to strip the /test prefix from the URI when it is sent to the backend. I tried to use the rewrite handler for this, but it does not work - the /test prefix is being sent to the backend when the request is forwarded.

I’m probably making a mistake somewhere in the JSON config when setting this up so I would appreciate any guidance on how to do this correctly.

4. Error messages and/or full log output:

N/A

5. What I already tried:

Read relevant docs - as far as I can tell the config file should be correct.

Update: I enabled debug logging and it seems like the rewrite handler is not running / making changes - the debug output only shows that the http.handlers.reverse_proxy ran and proxied the request. Looking at the relevant code in rewrite.go I should see some output from the rewrite module if a rewrite actually happened. As far as I can tell, the rewrite should be occurring…

I’ve also tried using “strip_path_prefix” but that did not help either. Any suggestions would really be appreciated!

I’m not sure if this helps. But this is what I use in the caddyfile (not json). Perhaps you can adapt it?

route /rct/* {
      uri strip_prefix rct
      reverse_proxy  localhost:6439 {
          header_up X-Real-IP {remote}
      }
}
1 Like

@vari You skipped some of the questions in the template :frowning: How can we reproduce the problem you’re seeing?

The problem was that the output wasn’t giving my anything apart from the reverse proxy handler attempting to handle the non-stripped path so I figured that output wouldn’t be useful. In hindsight that was probably not a great decision on my part :sweat:

I tested my config again and it seems to be working fine now. The only thing I modified was changing the matcher path to "/test" instead of "/test*". Not sure why that fixes the issue, but things work now so I’ll be leaving the config alone.

No worries, thanks for clarifying!

Huh, but your config posted above already has a matcher of /test* (with the asterisk). And indeed that should work.

Path matching is exact, unless you suffix with a * and then it is a prefix match: Request matchers (Caddyfile) — Caddy Documentation

PS. You can replace your whole uri_substring rewrite with "strip_path_prefix": "/test" since what you currently have doesn’t guarantee it will replace only the prefix. And it’s simpler.

1 Like

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