Rewrite / Proxy

Hi Everyone,
Thanks for the really cool project, and it is really nice that JSON is now the main language for Caddy configuration.

I have been trying to setup a simple file_server with some basic proxy rules and I am currently stuck in the following proxy rule that I cannot map to the JSON configuration

The following webpack rule

	proxy: {
          '/api/*': {
	    target: 'http://0.0.0.0:9999/loadBalancer/',
	      	pathRewrite: { '^/api': '' }
	  }

Note that the target is a different machine. From what I read on some posts is that we cannot
proxy to a http://0.0.0.0/PATH ?

My current configuration is

{
  "admin": {
    "disabled": false,
    "listen": "0.0.0.0:9066"
  },
  "apps": {
    "http": {
      "http_port": 9065,
      "servers": {
        "srp16154lx": {
          "listen": [
            "0.0.0.0:9065"
          ],
          "routes": [
            {
              "handle": [
                {
                  "handler": "file_server",
                  "root": "./test_root",
                  "hide": [
                    "config"
                  ]
                } 
              ]
            }
          ]
        }
      }
    }
  }
}

I have been looking at JSON Config Structure - Caddy Documentation
but without an example it is a bit tricky

Thanks in advance,
Lucas

1 Like

The easiest thing to get started is to start from Caddyfile config and use the caddy adapt command to get the underlying JSON config if you need it.

I think what you need is this:

:9065 {
	handle_path /api* {
		rewrite * /loadBalancer{uri}
		reverse_proxy localhost:9999
	}
}

Which gets you this JSON:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":9065"
          ],
          "routes": [
            {
              "match": [
                {
                  "path": [
                    "/api*"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "rewrite",
                          "strip_path_prefix": "/api"
                        }
                      ]
                    },
                    {
                      "group": "group0",
                      "handle": [
                        {
                          "handler": "rewrite",
                          "uri": "/loadBalancer{http.request.uri}"
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "reverse_proxy",
                          "upstreams": [
                            {
                              "dial": "localhost:9999"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}
1 Like

Thanks that worked.

1 Like

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