php - 带有 Larvalel api Nginx 配置的 Nuxt 前端
问题描述
我在同一台服务器上有一个带有 Laravel API 的 Nuxt 应用程序,并且由于我使用的是 laravel api.php,因此我遇到了 nginx 将 /api 与 /api/api 复制的情况。
这是我的设置。我在站点启用的半链接上只有一个简单的配置。
server {
listen 80;
root /var/www/html/nuxt-apt-front/dist;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /api{
alias "/var/www/html/laravel-api/public";
try_files $uri $uri/ @api;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/laravel-api/public/index.php;
}
}
location @api {
rewrite /api/(.*)$ /api/index.php?/$1 last;
}
include /etc/nginx/sites-available/*.conf;
}
有什么想法我可能做错了吗?
理想情况下,我想路由到 /api for laravel
http://example.com/api/login/google
现在,如果我在下面有这个,它似乎可以工作。
http://example.com/api/api/login/google
api.php 示例
// 通过谷歌登录登录。
Route::get('login/google', [GoogleAuthController::class, 'redirect']);
Route::get('login/google/callback', [GoogleAuthController::class, 'callback']);
解决方案
如上所述,如果您想保持 Nginx 文件原样,请更改以下代码RouteServiceProvider
:
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
对此:
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
这有点自我解释,前缀被添加到每个 api 路由/调用中。通过删除它,它就被简单地忽略了,只有你的 Nginx 配置附加的一个。
推荐阅读
- jquery - 如何在 JQuery 中使用每个并在 html 表中显示
- python - 如何遍历我的 for 循环的每个元素来获取这个 json 的值?
- javascript - 获取解析错误:意外的令牌,预期的“;”
- c# - ASMX SOAP Web 服务创建复杂类型而不是简单类型
- php - 如何使用旧版 Zend Framework 2 创建 wamp 3.1.9 开发环境
- c++ - C++ 导出函数中有多少个下划线('_')
- amazon-web-services - AWS CloudWatch Cron 表达式计划未触发 Lambda 函数
- uml - 用例之间的关系
- swift - UITableViewCell 设置 contentView backgroundColor 多于一个section
- c - 无法通过函数打印矩阵元素