cms icon indicating copy to clipboard operation
cms copied to clipboard

Control Panel Doesn't Load Properly on Non-standard Port

Open micchickenburger opened this issue 3 years ago • 7 comments

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.

image

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

  1. Use above configuration
  2. 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

micchickenburger avatar Mar 04 '22 23:03 micchickenburger

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.)

brandonkelly avatar Mar 07 '22 16:03 brandonkelly

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.

micchickenburger avatar Apr 06 '22 16:04 micchickenburger

Have you overridden your resourceBaseUrl config setting by chance?

brandonkelly avatar Apr 06 '22 17:04 brandonkelly

Nope, what you see above is my configuration.

micchickenburger avatar Apr 06 '22 18:04 micchickenburger

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',
    ],
  ],
];

brandonkelly avatar Apr 06 '22 21:04 brandonkelly

I tried both to the same effect. :-/ Is there some way I can be more helpful? Some other configuration I can share?

micchickenburger avatar Apr 07 '22 03:04 micchickenburger

Inspect the initial webpage request – does it specify the port in the Host header?

For example, here’s a request for port 8080:

Screenshot of a web request’s details, highlighting that the Host header i

brandonkelly avatar Apr 07 '22 16:04 brandonkelly