首页 > 解决方案 > 带有 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']);

标签: phplaravelubuntunginxnuxt.js

解决方案


如上所述,如果您想保持 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 配置附加的一个。


推荐阅读