[Docker] Help with react-router paths

1. The problem I’m having:

I have a dockerized react-vite app using react-router-dom, my domain name is https://nascent.dev.
When I try to access paths such as /library or /chapter (so https://nascent.dev/library) for example I get the page I am looking for, but the problem arises when I refresh the page or try to access it using the URL I get a blank page with a 404 error.

I looked into it and it was recommended to append file_server to the Caddyfile or even the try_files {path} /index.html command but unfortunately I don’t know how I can do this with a containerized environment.

2. Error messages and/or full log output:

404 - The requested path could not be found

3. Caddy version:

alpine2.7.5

a. System environment:

docker ubuntu, stack: react, react-router, vite

b. Caddyfile:

nascent.dev {
    reverse_proxy nascent-frontend:8080 {
        header_down Strict-Transport-Security max-age: 31536000
    }
}

c. docker-compose.yml file:

version: '3'

services:
  nascent-api:
    container_name: nascent-api 
    restart: unless-stopped
    build:
      context: ./bai
      dockerfile: Dockerfile
    ports:
      - "3000:3000"

  nascent-frontend:
    container_name: nascent-frontend 
    restart: unless-stopped
    build:
      context: ./mythril
      dockerfile: Dockerfile
    ports:
      - "8080:8080"

  caddy:
    image: caddy/caddy:2.7.5-alpine
    container_name: caddy-nascent
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:            
    - $PWD/Caddyfile:/etc/caddy/Caddyfile
    - $PWD/site:/srv
    - caddy_data:/data
    - caddy_config:/config

volumes:
  caddy_data:
  caddy_config: 

Yeah, try_files can only be used when Caddy has access to the files, but you’re using reverse_proxy here so that’s not possible.

You’ll need to configure your upstream server. I think you need to configure appType. See Shared Options | Vite

1 Like

Wow, I feel so dumb right now. I was using the npm package serve to have my front-end running which caused all this.

Thank you for highlighting that the problem might be related to vite rather than Caddy otherwise I’d have unnecessarily spend more time on it! lol

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