Bad Gateway 502 or 200 Blank Page Response when trying to use Invoice Ninja in combination with Caddy

Hey!

After several hours of troubleshooting including searching the internet for answers I’m out of ideas.
I would appreciate any support to get Invoice Ninja to run.
I still assume that it is a mix of config error between IN and Caddy. Maybe even mariadb. Sadly none of the logs give me any hints that something is wrong.
The only thing I see is either a 200 Response with a blank page (when Caddy uses php_fastcgi) or a 502 Bad Gateway with a blank page (when Caddy uses reverse_proxy)

Used Invoice Ninja Version: 5.6.19 (Docker Image)
Docker Version: 20.10.5
OS: Debian 11 (VServer)
MariaDB Version: 11.0.2 (Docker Image)
Used Caddy Version: 2.6.4 (Docker Image)

Additionally: Portainer (Docker Image) runs on the ports 8000 and 9443 which got started outside of the docker-compose file below

docker-compose.yml

version: "3.7"

services:
  caddy:
    image: caddy:latest
    container_name: caddy-test
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - caddy-config:/config
      - caddy-data:/data
      - /abs_path/caddy_server_files:/var/www/html
      - /abs_path/Caddyfile:/etc/caddy/Caddyfile
    networks:
      - custom
    
  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD_FILE: /run/secrets/db_pw
      MARIADB_DATABASE: "ninja"
    volumes:
      - mariadb-data:/var/lib/mysql
    networks:
      custom:
        ipv4_address: 172.18.0.250
    secrets:
      - db_pw

  invoice-ninja:
    image: invoiceninja/invoiceninja:latest
    container_name: ninja
    restart: always
    depends_on:
      - caddy
      - mariadb
    ports:
      - "7071:80"
    environment:
      APP_ENV: "production"
      APP_DEBUG: 1
      APP_URL: "https://invoice.domain.X"
      APP_KEY_FILE: /run/secrets/invoice_key
      APP_CIPHER: "AES-256-CBC"
      APP_LOCALE: "de"
      DB_TYPE: "mysql"
      DB_STRICT: 0
      DB_HOST1: "172.18.0.250"
      DB_DATABASE1: "ninja"
      DB_USERNAME1: "root"
      DB_PASSWORD1_FILE: /run/secrets/db_pw
      REQUIRE_HTTPS: 1
      IS_DOCKER: 1
      TRUSTED_PROXIES: "172.18.0.0/24"
    volumes:
      - ninja-public:/var/app/public
      - ninja-storage:/var/app/storage
    secrets:
      - invoice_key
      - db_pw
    networks:
      custom:
        ipv4_address: 172.18.0.253

volumes:
  caddy-config:
  caddy-data:
  mariadb-data:
  ninja-public:
  ninja-storage:

secrets:
  invoice_key: 
    file: /abs_path/invoice_key.txt
  db_pw:
    file: /abs_path/db_pw.txt
    
networks:
  custom:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 172.18.0.0/24

Caddyfile

{
    acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
}

domain.X {
    root * /var/www/html
    file_server
}

invoice.domain.X {
    reverse_proxy 172.18.0.253:7071
}

Things I already tried in various combinations always with a fresh install (removing all volumes and the container stack from docker compose)
Invoice Ninja:

* APP_URL with local IP
* APP_URL with http, https and without http/https at all
* REQUIRE_HTTPS turned on and off
* Used Trusted Proxies, also with *
* Instead of Port 80 (Http) piping to 443 (https)
* Changed from DB_HOST to DB_HOST1 (Including the other DB related environment variables)

Caddy:

* Instead of reverse_proxy using php_fastcgi
* various URLs and IPs

Things that were broken but didn’t fix the problem:

* App_Key file was not correct

Expected Behaviour
People can see the normal website behind the domain (which currently works) meanwhile the subdomain invoice.domain.X shows the IN interface. Being able to add more subdomains for other services.

Hi,

@david do you have any suggestions?

Sorry, i have zero experience with Caddy unfortunately, but a 502 is either the backend failing, or the request not being able to be passed to the backend.

@IsamuKondera - check the configs in S3 storage with Docker instance of Invoice Ninja v5 - #9 by sdggq

1 Like

I have the same issue using invoiceninja and caddy docker, can some one help to look into it and provide a solution?

@zyf You can check my previous post in the thread and find working configs. Compare them with yours.
Also, make sure to verify the chmod and chown is 1500 recursively on your public and storage folders for the ninja.