如何将 api.example.com 路由到 example.com/api 这样我就可以
api.example.com/v1/users
比使用
example.com/api/v1/users.
我正在使用nginx,谢谢。
最佳答案
确保这两个步骤到位。
检查您的 nginx 配置 /etc/nginx/conf.d/example.conf
并将域包含在 server_name
中像这样:
server_name example.com api.example.com;
检查您是否在 routes/api.php
中设置了路由文件。使用子域组是可选的,但请确保您拥有正确的路由。
使用域组的示例:
Route::group(['domain' => 'api.example.com'], function () {
Route::get('/v1/users', ['as' => 'api.users.index', 'uses' => 'UserController@index']);
}
不使用域组的示例 并允许两个 URL 指向同一个 Controller (确保按照 'as'
定义自己的路由名称)。
Route::get('/v1/users', ['as' => 'api.users.index', 'uses' => 'UserController@index']);
Route::get('/api/v1/users', ['as' => 'users.index', 'uses' => 'UserController@index']);
更新 :
关于子域路由的使用,请引用 Laravel 5.3 官方文档 https://laravel.com/docs/5.3/routing#route-group-sub-domain-routing
关于api - Laravel 5.3,使用 api.example.com 到 example.com/api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443753/