Docker环境下Apache子域名虚拟主机配置故障排查求助
Let's break down the issues with your Apache virtual host setup and get those subdomains working properly!
1. Wrong <VirtualHost> Directive Format
Your non-working virtual host configs (like backoffice.website.conf) use <VirtualHost backoffice.website:80> — this is incorrect. The <VirtualHost> directive specifies the IP address and port Apache listens on, not the domain name. Apache routes requests to the right virtual host using the Host header from the browser, matched against the ServerName you define.
Fix your subdomain configs to use the same format as your working localhost.conf:
<VirtualHost *:80> ServerAdmin admin@website.ee DocumentRoot /var/www/dev/public_html/backoffice ServerName backoffice.website <Directory /var/www/dev/public_html/backoffice> Options FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /var/log/apache2/backoffice.error.log LogLevel warn CustomLog /var/log/apache2/backoffice.access.log combined RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA] </VirtualHost>
I added logging directives here too — they'll help you debug issues later if something goes wrong.
2. Map Subdomains to Your Local Machine in Hosts File
Your computer doesn't know where backoffice.website or api.website point to. You need to add entries to your local hosts file to map these domains to your loopback address (127.0.0.1):
- Windows: Edit
C:\Windows\System32\drivers\etc\hosts(requires admin access) - macOS/Linux: Edit
/etc/hosts(usesudoto edit)
Add these lines:
127.0.0.1 backoffice.website 127.0.0.1 api.website 127.0.0.1 health.website
3. Access Using the Correct Port
Your Docker Compose maps container port 80 to host port 8080, so you need to include the port when accessing your subdomains. For example:
http://backoffice.website:8080
If you want to avoid typing the port, change the port mapping in docker-compose.yml to 80:80 (make sure no other service is using port 80 on your host first):
ports: - 80:80
4. Reload Apache Configuration
After making these changes, restart your Docker container to load the updated Apache configs:
docker-compose down && docker-compose up --build
You can also verify the Apache config syntax inside the container to catch any errors:
docker exec -it <your-web-container-name> apache2ctl configtest
If you see Syntax OK, you're good to go. If not, follow the error messages to fix typos or config issues.
Why Did localhost Work?
localhost is a default system domain that's already mapped to 127.0.0.1 in your hosts file, and your localhost.conf uses the correct <VirtualHost *:80> format with matching ServerName, so requests to http://localhost:8080 were routed correctly.
内容的提问来源于stack exchange,提问作者Nikita P

