I want to proxy /api to http://localhost:7000 with basicauth protect, but
basicauth not work in this situation, I don’t know how to config basic auth in route
sorry , I missed :8081{}
by this config, when I request /api/*, the reverse_proxy works, and responsed with correct data. But my browser did not propmt a auth dialog.
I suggest you read the docs for the route directive to understand what it does:
Directives built-in to Caddy are ordered according to this predetermined directive order. Directives from plugins do not get assigned an order, so you need to use either route or the order global option to assign them one.
When you placed basicauth at the end of route, then the reverse_proxy directive ran first and prevented basicauth from running, because it’s a terminal handler (i.e. no other handlers run after that one if it runs).
I think you want to have rate_limit before basicauth though, because otherwise Caddy will check the password before checking the rate limits, and checking passwords is relatively expensive in terms of CPU workload.
{
order after basicauth
}
:8081 {
rate_limit /api/* {remote_host} 5r/m
basicauth /api/* {
Bob JDJhJDEwJEVCNmdaNEg2Ti5iejRMYkF3MFZhZ3VtV3E1SzBWZEZ5Q3VWc0tzOEJwZE9TaFlZdEVkZDhX
}
reverse_proxy /api/* http://localhost:7000
}
Also I don’t think you had the right placeholder (i.e. {remote.host}) so I fixed it to {remote_host}. See the list of Caddyfile placeholder shortcuts here: Caddyfile Concepts — Caddy Documentation