How to Configure Caddy to proxy routes corresponding to image files to the backend and serve from there

1. Caddy version (caddy version):

caddy:2.1.1

2. How I run Caddy:

I run Caddy using docker here is my docker compose file.

a. System environment:

ubuntu : 18.04
Docker

b. Command:

docker build \
	-t chemmy-fashion-frontend-prod:prod \
	--build-arg CADDYFILE=Caddyfile \
    --build-arg BASE_URL=https://www.chemmyfashion.com/ \
    --build-arg NODE_ENV=production \
	-f Dockerfile .

c. Service/unit/compose file:

version: "3"
services:
fashion-frontend:
image: fashion-frontend-prod:${ENV}
restart: unless-stopped
ports:
- "80:80"
- "443:443"
networks:
- fashion-app
volumes:
- caddy-data:/data
- caddy-config:/config
fashion-backend:
image: fashion-backend
restart: unless-stopped
env_file: ./fashion-backend/config/${ENV}.env
ports:
- "5000:5000"
networks:
- fashion-app
volumes:
- /var/log/services/fashion-backend/:/var/log/services/fashion-backend/
- /root/treble/uploads/fashionuploads/:/usr/src/app/fashionuploads
networks:
fashion-app:
driver: bridge
volumes:
mongo-data:
driver: local
caddy-data:
driver: local
caddy-config:
driver: local

And here is my frontend project docker file

### first Stage

FROM node:14-slim AS builder

WORKDIR /usr/src/app

COPY ./package.json ./
COPY ./yarn.lock ./

RUN npm install

COPY . .

ARG BASE_URL
ENV REACT_APP_BASE_URL=${BASE_URL} NODE_ENV=${NODE_ENV}

RUN yarn build

### second stage

FROM caddy:2.1.1

ARG CADDYFILE
COPY ${CADDYFILE} /etc/caddy/Caddyfile

COPY --from=builder /usr/src/app/build/ /srv

EXPOSE 80

EXPOSE 443

d. My complete Caddyfile or JSON config:

www.chemmyfashion.com:443 {
    tls treblesolution@gmail.com
    root * /srv
    route {
        reverse_proxy /api* fashion-backend:5000
        try_files {path} {path}/ /index.html
        file_server
    }
}

3. The problem I’m having:

I have dockerize my MERN application.
as 2 separate project (backend and frontend)
When build (using docker) locally uploaded images (.png files) are loaded to the site without any issue.But after I configured my domain and built the application on digitalocean (droplet) uploaded images are not loading.

when I build (docker) locally I can access the image from this path : http://localhost:5000/fashionuploads/2022-01-04T12-59-56.790Z-shirt.png

but after build this production which configured on to domain I couldn’t able to load the image this path

https://www.chemmyfashion.com/fashionuploads/2022-01-04T14-24-29.772Z-images.jpeg

I am using
<img src=""> html tag to load the image from frontend.
I am very new to using Caddy server. Can anyone help me out to solve this problem I am struggling this with 2 days but couldn’t able to find a solution

N.B. : I have 2 seperate docker compose and docker files for local build and production build. I only put my production docker and caddy files here because of to reduce the content

4. Error messages and/or full log output:

No errors. But on client side images are not loading form backend only image path show as following

https://www.chemmyfashion.com/fashionuploads/2022-01-04T14-24-29.772Z-images.jpeg

5. What I already tried:

I google it but couldn’t able to find proper solution

6. Links to relevant resources:

That’s quite an old version. Please upgrade to v2.4.6!

Where are the image files? Are they copied into the container at /srv/fashionuploads? That’s where the file_server will look for them. Are you uploads stored elsewhere? Maybe you need to mount them as a volume to your container?

Hi @francislavoie,
You saved my life. I have mounted my volume to container (the path you mentioned). And thank for your explanation now I understood from where it’s searching image files. And also I have updated my caddy version to 2.4.6 (Which you have mentioned)
I need one more clarification and one advice from you
What I need to clarify is:
earlier there was an TLS handshake error for my chemmy.com
TLS handshake error from 198.41.242.110:64186: no certificate available for 'chemmyfashion.com'

for resolve this I have run this command from my server
curl -v --resolve www.chemmyfashion.com:<IPv4 ADDRESS OF MY SERVER> https://www.chemmyfashion.com/
I need to clarify is this the correct way to resolve this problem?
I got the solution form this community. ( but sometimes still I get this TLS handshake error)

The advice I need is
this website serve only you type : www.chemmyfashion.com
if you type chemmyfashion.com it will give an host error nothing serve from frontend. can you please give an advice how solve this issue as well. It’s really appreciate.

Thanks again for your help.

You’ll need to add another site block to your config to handle that domain. If you want the www. to be the authoritative one, then you can set up the apex to redirect to www.

chemmyfashion.com {
	redir https://www.chemmyfashion.com{uri}
}

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