I have a Nuxt application with a Laravel API on the same server and I am running into a situation where nginx is duplicating the /api with /api/api since I am using laravel api.php.
Here is my setup. I have just a simple conf under sites-available on a semilink to 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;
}
Any ideas what I could be doing wrong?
ideally I would like to route to /api for laravel
http://example.com/api/login/google
right now if I had this below it seems to work.
http://example.com/api/api/login/google
Example of api.php
// Login through google sign-in.
Route::get('login/google', [GoogleAuthController::class, 'redirect']);
Route::get('login/google/callback', [GoogleAuthController::class, 'callback']);
CodePudding user response:
As commented above if you want to keep the Nginx File as it is, change the following code in your RouteServiceProvider
:
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
to this:
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
It is somewhat self explaining, the prefix is added to each api route/call. By removing it, it is simply left out and there is only the one appended by your Nginx Config.