How to specify path for reverse proxy

1. The problem I’m having:

I’m using docker compose with asp net app and caddy
My config:

{
  "handle": [
    {
      "handler": "subroute",
      "routes": [
        {
          "handle": [
            {
              "handler": "reverse_proxy",
              "upstreams": [
                {
                  "dial": "constests:4000/contest/examplecontest"
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "match": [
    {
      "host": [
        "examplecontest.com"
      ]
    }
  ],
  "terminal": true
}

I want that if a user goes to the examplecontest.com site it will be proxied through constests:4000/contest/examplecontest, but it doesn’t seem to work.

How can this be fixed? The point is that I need to dynamically bind domains to a specific path via the api on the site

2. Error messages and/or full log output:

"msg":"dial tcp: lookup contest/examplecontest: no such host"
"err_trace":"reverseproxy.statusError (reverseproxy.go:1248)"

3. Caddy version:

2.7.4

4. How I installed and ran Caddy:

Via Docker compose

a. System environment:

Docker

b. Command:

c. Service/unit/compose file:

d. My complete Caddy config:

{
	"admin": {
		"listen": ":2019"
	},
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":443"
					],
					"routes": [
						{
							"handle": [
								{
									"handler": "subroute",
									"routes": [
										{
											"handle": [
												{
													"handler": "reverse_proxy",
													"upstreams": [
														{
															"dial": "contestgenerator:5000"
														}
													]
												}
											]
										}
									]
								}
							],
							"match": [
								{
									"host": [
										"contestcreator.com"
									]
								}
							],
							"terminal": true
						},
						{
							"handle": [
								{
									"handler": "subroute",
									"routes": [
										{
											"handle": [
												{
													"handler": "reverse_proxy",
													"upstreams": [
														{
															"dial": "contestgenerator:5000/contest/MZVPC_1234:80"
														},
													]
												}
											]
										}
									]
								}
							],
							"match": [
								{
									"host": [
										"examplecontest.com"
									]
								}
							],
							"terminal": true
						}
					]
				}
			}
		}
	}
}

5. Links to relevant resources:

As I said on How to specify path for reverse proxy · Issue #5817 · caddyserver/caddy · GitHub you need to use the rewrite handler.

I tried something like

{
  "handle": [
    {
      "handler": "subroute",
      "routes": [
        {
          "group": "group1",
          "handle": [
            {
              "handler": "rewrite",
              "uri": "contestgenerator:5000/contest/examplecontest"
            }
          ]
        }
      ]
    }
  ],
  "match": [
    {
      "host": [
        "examplecontest.com"
      ]
    }
  ],
  "terminal": true
}

But it doesn’t work, when i go to examplecontest.com it’s return 200 ok with no content

Rewrites don’t take a full URL, it only takes a path+query. Please read the documentation.

And you can’t only do a rewrite, you still need to proxy afterwards. Handlers are a chain.

Is there any particular reason you’re using a JSON config? You’d probably have an easier time with a Caddyfile.

Either way, you can write a Caddyfile for what you want, then run caddy adapt --pretty to get the equivalent JSON config.

i need to to dynamically add domains in my site. Example, i have domain contests.com, i want the content of contests.com/contest/example to be shown when navigating from another domain examplecontest.com

Yes, you need to use a rewrite to change the path, and the proxy to send the request to another server.

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