CentOS7 Apache环境下Laravel子域名配置无法访问问题求助
CentOS7 Apache环境下Laravel子域名无法访问问题排查与解决
问题概述
CentOS7服务器的Apache虚拟主机环境中部署Laravel项目,主站example.com访问正常,但所有子域名页面均无法访问。已配置子域名虚拟主机、尝试通配符配置、添加hosts记录,本地路由测试正常,问题仍未解决。
原虚拟主机配置
<VirtualHost *:80> ServerName example.com Redirect / https://example.com </VirtualHost> <VirtualHost *:443> ServerName example.com ServerAlias example.com DocumentRoot /var/www/html/example/public ErrorLog /var/www/html/example/storage/logs/error.log CustomLog /var/www/html/example/storage/logs/requests.log combine SSLEngine on SSLCertificateFile /etc/ssl/example/certificate.crt SSLCertificateKeyFile /etc/ssl/example/private.key SSLCertificateChainFile /etc/ssl/example/ca_bundle.crt <Directory /var/www/html/example> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
配置子域名后的虚拟主机(含通配符尝试)
<VirtualHost *:80> ServerName example.com Redirect / https://example.com </VirtualHost> <VirtualHost *:443> ServerName example.com ServerAlias example.com DocumentRoot /var/www/html/example/public ErrorLog /var/www/html/example/storage/logs/error.log CustomLog /var/www/html/example/storage/logs/requests.log combine SSLEngine on SSLCertificateFile /etc/ssl/example/certificate.crt SSLCertificateKeyFile /etc/ssl/example/private.key SSLCertificateChainFile /etc/ssl/example/ca_bundle.crt <Directory /var/www/html/example> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName subdomain.example.com Redirect / https://subdomain.example.com </VirtualHost> <VirtualHost *:443> ServerName subdomain.example.com ServerAlias subdomain.example.com #ServerAlias *.example.com DocumentRoot /var/www/html/example/public ErrorLog /var/www/html/example/storage/logs/error.log CustomLog /var/www/html/example/storage/logs/requests.log combine SSLEngine on SSLCertificateFile /etc/ssl/example/certificate.crt SSLCertificateKeyFile /etc/ssl/example/private.key SSLCertificateChainFile /etc/ssl/example/ca_bundle.crt <Directory /var/www/html/example> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
路由配置
Route::middleware(['web', 'auth']) ->domain('subdomain.'.config('app.url')) ->as('subdomain.') ->group(base_path('routes/subdomain.php')); Route::middleware(['web']) ->group(base_path('routes/web.php'));
排查与修复步骤
1. 优化Apache虚拟主机配置
当前配置存在冗余,且通配符未生效,可调整为统一处理主域名与所有子域名的配置:
<VirtualHost *:80> ServerName example.com ServerAlias *.example.com # 动态跳转到对应HTTPS域名 Redirect / https://%{HTTP_HOST} </VirtualHost> <VirtualHost *:443> ServerName example.com ServerAlias *.example.com DocumentRoot /var/www/html/example/public ErrorLog /var/www/html/example/storage/logs/error.log CustomLog /var/www/html/example/storage/logs/requests.log combine SSLEngine on SSLCertificateFile /etc/ssl/example/certificate.crt SSLCertificateKeyFile /etc/ssl/example/private.key SSLCertificateChainFile /etc/ssl/example/ca_bundle.crt # 目录指向Laravel的public目录,符合部署规范 <Directory /var/www/html/example/public> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
2. 验证SSL证书兼容性
若当前证书仅绑定example.com,子域名会触发SSL验证失败。通过以下命令检查证书的SAN字段:
openssl x509 -in /etc/ssl/example/certificate.crt -text -noout | grep -A 10 "Subject Alternative Name"
若输出中没有DNS:*.example.com,需重新申请通配符SSL证书。
3. 修正Laravel路由配置
config('app.url')通常包含协议(如https://example.com),会导致生成无效的子域名格式。修改路由配置:
// 提取不带协议的纯域名 $domain = parse_url(config('app.url'), PHP_URL_HOST); Route::middleware(['web', 'auth']) ->domain('subdomain.' . $domain) ->as('subdomain.') ->group(base_path('routes/subdomain.php')); Route::middleware(['web']) ->group(base_path('routes/web.php'));
同时确保.env文件中APP_URL设置正确:
APP_URL=https://example.com
4. 验证Apache服务配置
- 启用必要模块:
sudo a2enmod rewrite ssl vhost_alias
- 检查配置语法:
sudo apachectl configtest
- 重启Apache服务:
sudo systemctl restart httpd
5. 确认DNS/Hosts记录
- 服务器端
/etc/hosts添加记录:
你的服务器IP example.com subdomain.example.com
- 本地测试时,修改本地
hosts文件(Windows为C:\Windows\System32\drivers\etc\hosts,Linux/macOS为/etc/hosts)添加相同记录,避免DNS缓存干扰。
6. 清理Laravel缓存
修改配置后清理缓存,确保路由与配置生效:
cd /var/www/html/example php artisan cache:clear php artisan route:clear php artisan config:clear
内容的提问来源于stack exchange,提问作者SpamBug
相关产品推荐
相关产品推荐

