I am trying to move my local testing site into a docker container for ease of use when changing between machines. I am using caddy within docker so that I can connect to the locally hosted site via https.
It is all working well except for the fact that cakephp uses the php $_SERVER['HTTPS']
variable when creating links using Router::url
. Since caddy creates a reverse proxy on port 80 to the php server, the $_SERVER variables 'SERVER_PORT' and 'HTTPS' in php are being set to 80 and null respectively, even when the original request was made out to an https:// url.
How can I get the php $_SERVER variables 'SERVER_PORT' and 'HTTPS' to reflect the values in the original request (pre-proxy).
Options I have considered:
- Overriding and hard coding the environment variables which php uses for 'SERVER_PORT' and 'HTTPS'.
- Making the caddy reverse proxy connect to the php server on port 443
- Working out whether the request was made via https by looking at the url (or some other method)
The issue with first 2 options is that if a request is made via the http protocol php will believe it was made via https posing a security issue (and opening us up to annoying bugs when pushing to production).
The issue with the final option is that I don't want to screw around with the cakephp framework too much and there and this is likely not the only part of the website which uses these variables. It could also make future development a pain for developers who are not aware of the issues with using those server variables.
My setup (simplified):
Caddyfile:
local.site.com {
tls internal
reverse_proxy myWebsite:80
}
docker-compose.yml:
version: '3.8'
services:
caddy:
image: caddy:2-alpine
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./caddy/data:/data
- ./caddy/config:/config
ports:
- '80:80'
- '443:443'
myWebsite:
depends_on:
- caddy
build: .
volumes:
- /path/to/websiteCode:/var/www/html
Dockerfile:
FROM php:7.0-apache
Any help and advice is appreciated! Thanks.