craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.25k stars 629 forks source link

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

Open micchickenburger opened 2 years ago

micchickenburger commented 2 years ago

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

brandonkelly commented 2 years ago

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

micchickenburger commented 2 years ago

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.

brandonkelly commented 2 years ago

Have you overridden your resourceBaseUrl config setting by chance?

micchickenburger commented 2 years ago

Nope, what you see above is my configuration.

brandonkelly commented 2 years ago

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',
    ],
  ],
];
micchickenburger commented 2 years ago

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

brandonkelly commented 2 years ago

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