Control Panel Doesn't Load Properly on Non-standard Port
Description
The control panel doesn't seem to load resources properly when running craft on non-standard ports. The links all reference the correct port but resources like images, scripts, and stylesheets drop the port number.
It just tries to load resources from localhost even though I configured the BASE_CP_URL to be localhost:5000.
docker-compose.yml
version: '3'
services:
app:
build:
context: .
dockerfile: .docker-config/php/Dockerfile
expose:
- 9000
volumes:
- ./craft:/var/www/html
environment:
COMPOSER_ALLOW_SUPERUSER: 1
ENVIRONMENT: dev
SECURITY_KEY: security_key
DB_DSN: mysql:host=database;port=3306;dbname=craft
DB_USER: user
DB_PASSWORD: password
DEFAULT_SITE_URL: http://localhost:5000
BASE_CP_URL: http://localhost:5000
database:
build:
context: .
dockerfile: .docker-config/mysql/Dockerfile
volumes:
- ./craft/storage/db:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: craft
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8085:80
environment:
PMA_HOST: database
nginx:
build:
context: .
dockerfile: .docker-config/nginx/Dockerfile
ports:
- 5000:80
volumes:
- ./craft/web:/var/www/html/web
depends_on:
- app
config/general.php
<?php
return [
'*' => [
// Control Panel access via environment variable
'baseCpUrl' => craft\helpers\App::env('BASE_CP_URL') ?: null,
// Control Panel trigger word
'cpTrigger' => 'admin-login',
],
];
We do not use .env files.
Steps to reproduce
- Use above configuration
- Navigate to control panel
Additional info
- Craft version: 3.7.36
- PHP version: 8.1.3
- Database driver & version: MySQL 8.0+
- Plugins & versions: Will provide upon request
This appears to be a Yii bug. I’ve just submitted a bug report about it: yiisoft/yii2#19290
In the meantime, you can fix by manually defining your @web alias to http://localhost:5000. Add this to config/general.php:
'aliases' => [
'@web' => 'http://localhost:5000',
],
(Ideally the actual value should be defined by an environment variable, and you pull it in via craft\helpers\App::env('ENV_VAR_NAME'), as it’s going to change depending on the environment.)
Thanks for your reply. I tried this configuration but it did not change the outcome. The admin interface still seems to hardcode resources to port 80.
Have you overridden your resourceBaseUrl config setting by chance?
Nope, what you see above is my configuration.
Did you add the aliases key within the * array or at the top level? It should be:
return [
'*' => [
// Control Panel access via environment variable
'baseCpUrl' => craft\helpers\App::env('BASE_CP_URL') ?: null,
// Control Panel trigger word
'cpTrigger' => 'admin-login',
'aliases' => [
'@web' => 'http://localhost:5000',
],
],
];
I tried both to the same effect. :-/ Is there some way I can be more helpful? Some other configuration I can share?
Inspect the initial webpage request – does it specify the port in the Host header?
For example, here’s a request for port 8080:
