I use nginx as a reverse proxy for my Node.js application(s) which is running on localhost:3000. Both nginx itself and my application are two separate docker containers. (Separate because later I plan to add more apps to it.)
In my app container I have some .webp
alternative images. E.g.
$ ls
/app/assets/img/image.jpeg
/app/assets/img/image.jpeg.webp
/app/assets/img/other.jpeg
I've tried using try_files
with nginx but as far as I know this is not possible because try_files doesn't "see" my files inside another docker container unless they share a volume. Which they cant. The App container is built somewhere else with everything inside.
What I have right now it this:
upstream app {
server localhost:3000;
}
[...]
location ~* .+\.(jpg|jpeg|svg|png)$ {
proxy_pass http://app$uri$webp_suffix;
}
[...]
location / {
proxy_pass http://app;
}
This is in fact working. The client requests .jpg
image and gets .jpg.webp
(because he can handle it). The problem is he always gets .jpg.webp
. What I need is some if to check if the requested file is available in my app.
So something similar like this maybe? But this is not working :-(
location ~* .+\.(jpg|jpeg|svg|png)$ {
if (-f http://app$uri$webp_suffix) {
proxy_pass http://app$uri$webp_suffix;
}
proxy_pass http://app$uri;
}