Laravel如何从子域名URL中移除"api"前缀
嘿,我来帮你把这个Laravel应用的路由和Nginx配置规范地整理出来,方便你排查或参考:
场景概述
我搭建了一个Laravel应用,同时支持Web端功能和给安卓、iOS提供REST API服务,项目里有routes/api.php和routes/web.php两个路由文件,其中API路由绑定到了子域名api.example.com,同时配置了对应的Nginx服务块。
Laravel API路由配置(
routes/api.php) Route::group([ 'domain' => 'api.example.com' ], function(){ // 这里放置你的API路由规则,比如: // Route::get('/users', [UserController::class, 'index']); // Route::post('/login', [AuthController::class, 'login']); });
Nginx服务块配置
server { listen 80; listen [::]:80; root /var/www/laravel/public; index index.php; server_name api.example.com; location / { try_files $uri $uri/ /index.php?$query_string; # 注:你提供的配置内容在这里截断了,完整的try_files规则通常如上所示 } # 通常还需要添加PHP处理的location块,适配Laravel运行需求: location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据你的PHP版本调整路径 } }
小补充:如果你的Web端路由(routes/web.php)需要绑定到主域名example.com,可以在Nginx里再配置一个对应的server块,指向同一个Laravel项目的public目录,将server_name设为example.com www.example.com即可实现多域名分流。
内容的提问来源于stack exchange,提问作者Lakshmaji
相关产品推荐
相关产品推荐

