【问题标题】:Nuxt Front end with Larvalel api Nginx configuration带有 Larvalel api Nginx 配置的 Nuxt 前端
【发布时间】:2021-12-18 05:04:09
【问题描述】:

我在同一台服务器上有一个带有 Laravel API 的 Nuxt 应用程序,我遇到了一种情况,即 nginx 正在使用 /api/api 复制 /api,因为我使用的是 laravel api.php。

这是我的设置。我在sites-available 下有一个简单的conf,在sites-enable 的半链接上可用。

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;
}

有什么想法我可能做错了吗?

理想情况下,我想为 laravel 路由到 /api

http://example.com/api/login/google

现在,如果我在下面有这个,它似乎可以工作。

http://example.com/api/api/login/google

api.php 示例

//通过google登录方式登录。

Route::get('login/google', [GoogleAuthController::class, 'redirect']);
Route::get('login/google/callback', [GoogleAuthController::class, 'callback']);

【问题讨论】:

  • 你能显示你的api路由文件吗?
  • @Aless55 当然,我用路线示例更新了我的问题。
  • 默认的 laravel 配置是在 api 路由前加上 api。 AFAIK,您可以在 RouteServiceProvider 中更改此设置
  • @apokryfos 这就是问题所在。作者可以更改他们的 nginx 配置文件或 RouteServiceProvider 中的方法。你的情况真的有必要使用 nginx 配置吗?
  • @apokryfos 知道在 nginx 配置中处理它的方法吗?如果可能的话,宁愿在那里处理它

标签: php laravel ubuntu nginx nuxt.js


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 2016-11-11
    • 2020-09-24
    • 2020-01-31
    • 2020-04-12
    • 2019-09-17
    • 2021-05-19
    • 2016-11-21
    相关资源
    最近更新 更多