is it ok to use ktor as a reverse proxy for static...
# ktor
e
is it ok to use ktor as a reverse proxy for static content? any drawbacks?
d
It should be ok. But if it is just for that I would use nginx instead.
2
e
it’s going to be as a smart gateway with complex routing and app firewall for downstream services
d
If you cannot define the routing with nginx easily, you can consider using ktor indeed. But you have to know that there is a overhead. Ktor uses the JVM that has a GC and additional overhead while nginx is written in C and doesn’t have that overhead.
The best thing you can do is to do a benchmark to decide. If you can scale horizontally without too much trouble and you prefer ease to performance, it should be ok.
Iirc, nginx supported headers for reverse proxying for serving files directly. So you generate a header in your backend that was reverse-proxied by nginx, and nginx served the file without the overhead including range support. Maybe there is a header to reverse-proxy another backend, so you can generate a URL in your backend in a header, and instruct nginx to reverse-proxy it without any overhead. So you can define the logic in your backend and nginx do all the heavy work. Let me check if there is such header
For files, it is
X-Accel-Redirect
(similar to XSendFile): https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/ Now let’s check if it allows to do something to reverse-proxy an url too
and if you want to keep it simple, you can do it with ktor too, but I would do a PoC to see if the performance is good enough for you. You can also report us feedback about performance and we can try to improve it
e
yeah, looks good. should I also use nginx for regular web ktor applications to serve static content?
d
Not required. In my case for example, I have docker and I use a companion container with nginx that handles certificates and domains and reverse-proxy each website using the domain
And in that case, I guess you can configure nginx to cache your static resources based on some headers
From example an etag. So you can serve normally with ktor, and further optimize it later if required with an nginx
I usually try to not do premature optimizations related to that, to keep things simple, but I understand that some use-cases might require special performance, so that’s why I suggested you to use nginx if you are not going to have too much logic
e
in this case I think it is better to serve content with CDN. ok, i’ll try to do it with ktor
👍 1