Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

navidbakhtiary's avatar

Getting error 502 for API calling on Laravel and Nginx

When I call my api "https://mydomain.com/api/v1", I get Nginx 502 error.

when I call my web url "https://mydomain.com/info", I get the right response.

For other nonexistence web routes, I get Laravel Not Found page that is correct.

this is the boot method of my RouteServiceProvider:

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api/v1')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api_v1.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

This is my Nginx server block:

server {
    listen 80;
    server_name bazar118.ir;
    set $sbhost "https://bazar118.ir";
    return 301 $sbhost$request_uri;
}
server {
    listen 443 ssl;
    server_name bazar118.ir;
    ssl_certificate /etc/letsencrypt/live/bazar118.ir/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/bazar118.ir/privkey.pem; # managed by Certbot

    location / {
        root /var/www/BackEnd/public;
        try_files $uri $uri/ /index.php?$query_string;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }
    }
}

I implemented the chatgpt solutions like those that put the both frontend and backend in one nginx config file, but still have 502.

I am using Laravel 9.

What did I do wrong or what did I forget?

Thank you for your help in advance.

0 likes
0 replies

Please or to participate in this conversation.