Nginx redirect in a Docker environment

Hi there,

I’m currently trying to add Kirby as a headless CMS in a Docker environment where I already have a Next.js frontend, a Laravel app with a MySQL database and a Matomo image.

I managed to add Kirby to my Docker Compose settings and to make it run on a localhost:8181 route (the rest of the application being on localhost:8080). Now I’d like to have Kirby as a subroute localhost:8080/kirby in order to have only one route to deal with at the end. But I don’t manage to see what kind of NGINX setup I need to apply (I tried to follow this cookbook but don’t really manage to apply it to my setup).and everything I tried ended up with a 502 Bad Gateway.

Has anybody already tried to do something like that? Thanks in advance for your help!

I think your situation might be clearer if you share the current nginx config and docker compose file.

For example, for me it’s not clear how you’re running Kirby. The cookbook you linked expects 2 containers: one for PHP fpm and one for nginx, but you mentioning “Kirby running on :8181” makes me believe there’s probably an apache server in that container? How are the frontend and Laravel apps working (are those two containers or one)?

In the end: frontend, Laravel and Kirby are all static files or PHP, you could also think to put everything in one container and just have Kirby in a kirby subfolder.

Otherwise, if Kirby’s already responding, you could probably just use nginx as reverse proxy:

location /kirby/ {
    proxy_pass http://kirbylinkname:8181/;
}

Having Kirby running behind a reverse proxy, probably also requires you to specifically set the “website url” option in the config url | Kirby CMS

Thanks @rasteiner, indeed, there were some problem with ports overall that I solved and I can now get access to the Kirby app from http://localhost:8080/kirby!

Though, not everything is solved yet… I can’t login to the panel because every file in the media folder lead to a 404, despite actually existing.

When I go to the http://localhost:8080/kirby URL, I can see the content of the home.php template, so the NGINX redirection seems to work so I can’t really understand what’s going on.

Here’s the NGINX redirection:

location /kirby {
    proxy_pass http://kirby:80;
}

The config.php file

<?php

return [
  'debug' => true,
  'url' => 'http://localhost:8080/kirby/'
];

And the docker-compose.yml file:

services:
  nginx:
    image: nginx
    container_name: nginx
    restart: unless-stopped
    env_file:
      - ./.env.development
    networks:
      - network
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx:/etc/nginx/templates
      - ./nginx/mime.types:/etc/nginx/mime.types
    depends_on:
      - kirby
    ports:
      - 8080:80

  kirby:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: docker-starterkit
    container_name: kirby
    networks:
      - network
    volumes:
      - ./kirby:/var/www/html/
    env_file:
      - ./.env.development

Can you check in the Kirby container logs what gets actually requested for the media files?

And one more update. I tried to modify the NGINX redirection to this by adding the trailing slashes:

location /kirby/ {
    proxy_pass http://kirby:80/;
}

I can now go to the panel and have no 404 anymore. But when I try to access this URL http://localhost:8080/kirby/panel/site, I get redirected to http://localhost:8080/panel/site which then becomes a 404 page when I reload the page.

I don’t have anything in the Kirby container log! Only in the NGINX one.

Hmm. Sounds like Kirby doesn’t understand its url, supposing the redirect comes from there. Maybe someone from the team (pinging @distantnative I think) knows more about why the panel redirects like this, even when having the url option set.

PS: I guess you have a current version of Kirby installed?

Wondering if this could be the same issue as Fix panel url redirect issue when running on subfolder by afbora · Pull Request #5313 · getkirby/kirby · GitHub - which got fixed for Kirby 3.9.6 which isn’t released yet.

@quentin-f451 would you feel comfortable enough with PHP to try out the two changes in the linked PR on your installation to see if that solves it?

Unfortunately, updating the two files modified by this PR didn’t seem to solve anything here… Still the same redirect with the trailing slashes, still the same 404 for everything in media folder without

I thought media was already loaded now, just the redirect.
In that case I’d assume it’s rather nginx not being correctly configured. But I have 0 expertise with nginx, so can’t really help there.

I think the media urls are working now. He tried two nginx configs:
first:

location /kirby {
    proxy_pass http://kirby:80;
}

and then:

location /kirby/ {
    proxy_pass http://kirby:80/;
}

The second one seems to be the right one (judging by nginx docs example). That one is also the version where media urls work, and that’s why I believed it’s Kirby doing the redirect.

Other than setting the url option, is there anything else to be done on Kirby’s side to get reverse proxies working?


@quentin-f451 Just to be sure that it’s Kirby which is redirecting, you could add something to your index.php, something like:

<?php 

/** @TODO: remove this line after tests */
header('X-Kirby: This is Kirby');

require 'kirby/bootstrap.php';

echo (new Kirby)->render();

Then go to the network tab in the Chrome Dev Tools, enable the “Preserve Logs” check, open the http://127.0.0.1:8080/kirby/panel page, in the network tab click on the “panel” request with the 30x status and check if there’s a response header of “X-Kirby: This is Kirby”.

Thanks for the advise! I just tried what you asked, and indeed it seems like the redirect comes from Kirby:

1 Like