V2: unrecognized directive: import (inside route)

1. Caddy version (caddy version):

v2.3.0-beta.1

2. How I run Caddy:

caddy start (just testing my Caddyfile before upgrading to v2)

a. System environment:

linux

d. My complete Caddyfile or JSON config:

my.domain {
  route /api/* {
    uri strip_prefix /api
    reverse_proxy :3000

    import vkapps-cors
  }

  route /bot/* {
    uri strip_prefix /bot
    reverse_proxy :3001
  }
}

3. The problem I’m having:

import directive doesn’t work inside route

4. Error messages and/or full log output:

run: adapting config using caddyfile: parsing caddyfile tokens for 'route': Caddyfile:60 - Error during parsing: unrecognized directive: import

Note

I moved my import statement above route, it works fine, but i figured i need to repost this bug somewhere. Is this intended?

Is vkapps-cors a file? I don’t see a snippet defined in the Caddyfile you posted. It works fine for me if I define a snippet.

Also, you can replace route and uri strip_prefix with just handle_path:

(vkapps-cors) {
	encode gzip
}

my.domain {
	handle_path /api/* {
		reverse_proxy :3000

		import vkapps-cors
	}

	handle_path /bot/* {
		uri strip_prefix /bot
		reverse_proxy :3001
	}

	handle {
		# Fallback for paths not otherwise handled
	}
}

Thanks for the tip! handle_path is much better and import works inside it.
Oops, i forgot the snippet:

(vkapps-cors) {
  header Access-Control-Allow-Headers origin,content-type,accept,x-vk-sign
}

But putting import inside route produced the error above.

I can’t reproduce that problem. This works fine to me:

Caddyfile:

(vkapps-cors) {
  header Access-Control-Allow-Headers origin,content-type,accept,x-vk-sign
}

my.domain {
  route /api/* {
    uri strip_prefix /api
    reverse_proxy :3000

    import vkapps-cors
  }

  route /bot/* {
    uri strip_prefix /bot
    reverse_proxy :3001
  }
}

:point_down:

$ caddy adapt --pretty
2020/12/10 21:07:15.202 INFO    using adjacent Caddyfile
{
        "apps": {
                "http": {
                        "servers": {
                                "srv0": {
                                        "listen": [
                                                ":443"
                                        ],
                                        "routes": [
                                                {
                                                        "match": [
                                                                {
                                                                        "host": [
                                                                                "my.domain"
                                                                        ]
                                                                }
                                                        ],
                                                        "handle": [
                                                                {
                                                                        "handler": "subroute",
                                                                        "routes": [
                                                                                {
                                                                                        "handle": [
                                                                                                {
                                                                                                        "handler": "subroute",
                                                                                                        "routes": [
                                                                                                                {
                                                                                                                        "handle": [
                                                                                                                                {
                                                                                                                                        "handler": "rewrite",
                                                                                                                                        "strip_path_prefix": "/api"
                                                                                                                                }
                                                                                                                        ]
                                                                                                                },
                                                                                                                {
                                                                                                                        "handle": [
                                                                                                                                {
                                                                                                                                        "handler": "reverse_proxy",
                                                                                                                                        "upstreams": [
                                                                                                                                                {
                                                                                                                                                        "dial": ":3000"
                                                                                                                                                }
                                                                                                                                        ]
                                                                                                                                }
                                                                                                                        ]
                                                                                                                },
                                                                                                                {
                                                                                                                        "handle": [
                                                                                                                                {
                                                                                                                                        "handler": "headers",
                                                                                                                                        "response": {
                                                                                                                                                "set": {
                                                                                                                                                        "Access-Control-Allow-Headers": [
                                                                                                                                                                "origin,content-type,accept,x-vk-sign"
                                                                                                                                                        ]
                                                                                                                                                }
                                                                                                                                        }
                                                                                                                                }
                                                                                                                        ]
                                                                                                                }
                                                                                                        ]
                                                                                                }
                                                                                        ],
                                                                                        "match": [
                                                                                                {
                                                                                                        "path": [
                                                                                                                "/api/*"
                                                                                                        ]
                                                                                                }
                                                                                        ]
                                                                                },
                                                                                {
                                                                                        "handle": [
                                                                                                {
                                                                                                        "handler": "subroute",
                                                                                                        "routes": [
                                                                                                                {
                                                                                                                        "handle": [
                                                                                                                                {
                                                                                                                                        "handler": "rewrite",
                                                                                                                                        "strip_path_prefix": "/bot"
                                                                                                                                }
                                                                                                                        ]
                                                                                                                },
                                                                                                                {
                                                                                                                        "handle": [
                                                                                                                                {
                                                                                                                                        "handler": "reverse_proxy",
                                                                                                                                        "upstreams": [
                                                                                                                                                {
                                                                                                                                                        "dial": ":3001"
                                                                                                                                                }
                                                                                                                                        ]
                                                                                                                                }
                                                                                                                        ]
                                                                                                                }
                                                                                                        ]
                                                                                                }
                                                                                        ],
                                                                                        "match": [
                                                                                                {
                                                                                                        "path": [
                                                                                                                "/bot/*"
                                                                                                        ]
                                                                                                }
                                                                                        ]
                                                                                }
                                                                        ]
                                                                }
                                                        ],
                                                        "terminal": true
                                                }
                                        ]
                                }
                        }
                }
        }
}

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