Repository: provectus/kafka-ui
Issue: Error when running behind a reverse proxy with a path prefix
Labels: type/bug
scope/backend
status/accepted
status/confirmed

**Describe the bug**
<!--(A clear and concise description of what the bug is.)-->
When using kafka-ui behind a reverse proxy (both nginx and trafik), trying to configure it on a sub-path (/kafka-ui) doesn't work without a trailing `/`.
Any attempt to reach kafka-ui ends up in a 404 through reverse proxy.
Reaching through direct port (8082) result in a json response with 404 message.
```json
{
  "code": 5000,
  "message": "404 NOT_FOUND",
  "timestamp": 1648462573453,
  "requestId": "936e888d-1",
  "fieldsErrors": null,
  "stackTrace": "org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND\n\tat org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$1(ResourceWebHandler.java:379)\n\tSuppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: \nError has been observed at the following site(s):\n\t*__checkpoint ⇢ com.provectus.kafka.ui.config.CustomWebFilter [DefaultWebFilterChain]\n\t*__checkpoint ⇢ com.provectus.kafka.ui.config.ReadOnlyModeFilter [DefaultWebFilterChain] (...)"
}
```

**Set up**
<!--
(How do you run the app? 
Which version of the app are you running? Provide either docker image version or check commit hash at the top left corner. We won't be able to help you without this information.)
-->
Docker for Windows with image `provectuslabs/kafka-ui:latest` (f1e30cd9d423)

**Steps to Reproduce**
Steps to reproduce the behavior:

1. Clone kafka-ui repo
2. Remove trailing `/` in `PathPrefix` in `documentation/compose/traefik/kafkaui.yaml`
3. Run `cd documentation/compose && docker compose --file kafka-ui-traefik-proxy.yaml up -d` in terminal
4. Browsing to `http://localhost/kafka-ui` results in 404

(Same issue with `kafka-ui-reverse-proxy.yaml`; its not a traefik config issue)

**Expected behavior**
<!--
(A clear and concise description of what you expected to happen)
-->
We should be able to run kafka-ui on a sub-path behind a reverse proxy with or without trailing `/`.
It's quite dumb, but I searched several hours to figure out my 404 was due to missing forward slash.
I'm pretty sure I'm not the first to shoot himself with this :)

**Additional context**
<!--
(Add any other context about the problem here)
-->
My use case is a development docker compose, with multiple UI's (our webapp, pgadmin, kafka-ui, ...) on different path prefix behind traefik.

I found this because my traefik rule is ``PathPrefix(`/kafka-ui`)``.

Looking at the stacktrace and current code on master branch, I think [`CustomWebFilter`](https://github.com/provectus/kafka-ui/blob/master/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/CustomWebFilter.java#L19) does not handle request properly when path is empty.

Thank you for your time :)

Best regards

Issue comments:
Hello there anthares! 👋

Thank you and congratulations 🎉 for opening your very first issue in this project! 💖

In case you want to claim this issue, please comment down below! We will try to get back to you as soon as we can. 👀
Hey, thanks for reaching out. I'll investigate.
Any updates on this? I'm encountering a similar problem. 

Here's my nginx config for kafka-ui:

```
  location /kafka-ui/ {
    proxy_set_header X-Script-Name /kafka-ui;

      proxy_pass http://kafka-ui:8081;
  }
```

Here's the error:
![image](https://user-images.githubusercontent.com/37439882/163708590-94c633f2-f648-45aa-aac7-a40c80d37efb.png)

Will be fixed within #1874 and will be a part of 0.4 release (or you can always pull the `master`-labeled image).
A bit late, but thank you @Haarolean for fixing this :)
> A bit late, but thank you @Haarolean for fixing this :)

You're welcome :)
I'm still running into this issue - which nginx configuration(s) are you using @Haarolean @anthares ?
I have not tested with nginx, since I'm using traefik as reverse proxy.
It do works with 0.4.0 with standard configuration, nothing fancy.

Maybe make sure you pulled the latest image, or check examples as they provide working configurations.

Pull request review comments:

Patch that resolved the issue:
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/CustomWebFilter.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/CustomWebFilter.java
index a74a3fa6a16..204fdfad7e2 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/CustomWebFilter.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/CustomWebFilter.java
@@ -16,7 +16,7 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
 
     final String path = exchange.getRequest().getPath().pathWithinApplication().value();
 
-    if (path.startsWith("/ui") || path.equals("/")) {
+    if (path.startsWith("/ui") || path.equals("") || path.equals("/")) {
       return chain.filter(
           exchange.mutate().request(
               exchange.getRequest().mutate()


Test patch: