Alias directive like in NGINX?

why not add directive like nginx alias, nginx can do this well。
for example, if the website like this: (the bar.jpg in the foo/bar/index.html like <img src="bar.jpg">, we should make sure the img can display correct)

foo
├── bar
│   └── index.html
│   └── bar.jpg
└── index.html

and the relations like this

example.com ====> foo/index.html
example.com/abc ====> bar/index.html

if use caddy:

example.com {
	root * /foo
	file_server

	redir /abc /abc/
	handle_path /abc/* {
		root * /foo/bar
		file_server
	}
}

if use nginx:

server {
        listen       80;
        server_name  example.com;

        location / {
            root   /foo;
        }
        location /abc/ {
            alias   /foo/bar/;
        }
    }   

I moved your comment into a new topic, because it was unrelated to the wiki topic you commented on – the article was specifically about proxying to apps whose code you don’t control.

That said, I’m not really sure what you’re asking here. Isn’t your Caddy config equivalent? All alias does in nginx is define an alternative root for the file server, taking into account the path rewrite that location implicitly performed. That distinction is not necessary in Caddy because you can choose to rewrite with handle_path or not with handle.

That said, I’d recommend writing your config like this:

example.com {
	redir /abc /abc/
	handle_path /abc/* {
		root * /foo/bar
		file_server
	}

	handle {
		root * /foo
		file_server
	}
}

The handle and handle_path directives are mutually exclusive, so only the first matching one will run. The handle has no matcher, so it’ll act as a fallback if no other handle matched.

2 Likes

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