Excuse some necromancy, but I was having the same problem and fixed it more or less, though maybe there is a better solution.
I am experimenting with two docker containers running Nginx. The first acts as a gate and listens to my domain. HTTP is exposed on port 8080. The second container runs a CakePHP app (version 4.x for me), exposed on port 8081 for testing and on the same network as the gate.
Now I would like to access example.local:8080/my_app
and see my CakePHP app, through a reverse proxy.
For the second container I have this conf.d/default.conf
:
server {
listen 80 default_server;
server_name _;
# Nginx root must point to application webroot:
root /var/www/html/webroot;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.htaccess {
deny all;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass uptime-php:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
This simply offers the Cake application. Vising the debug port localhost:8081
now shows the application working.
Now for the gate I have the following entry in my conf.d/default.conf
:
server {
listen 80;
server_name example.local www.example.local;
location /my_app {
rewrite /my_app/(.*) /$1 break;
proxy_pass http://my_app-web/;
proxy_redirect http://$host/ /my_app/;
proxy_set_header Host $host;
}
}
(Note that my_app
is the container name, and internal domain name, of the CakePHP container.)
This sets up the reverse proxy under a subdirectory.
(Note that I use a local DNS entry to point example.local
to 127.0.0.1
.)
Visiting example.local:8080/my_app
now kind of works, but all CSS and JS are missing and all redirects are wrong. CSS etc. are loaded as example.local:8080/css/*
(missing subfolder) and redirects are done to example.local/my_app/*
(wrong port). This might make sense, after the rewrite the app containers receives a different URL.
I fixed this (more or less) by adding these config entries, overriding some automatic detection:
// app_local.php
// ...
'App' => [
'base' => '/my_app',
'webroot' => '/var/www/html',
'fullBaseUrl' => 'http://example.local:8080',
],
// ...
Now example.local:8080/my_app
works in full!
But localhost:8081
doesn’t work any more (because the URL and base don’t match).
My question: can we fix by improving our rewrite / reverse proxy settings instead, such that the CakePHP URL detection is correct?