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

Yahrod's avatar
Level 1

404 error with Render

Hello everyone, I have some issue to reach my Laravel API hosted on Render. Everything works fine in localhost (http: //localhost:8000/api/) but when I try with the Render url (https: //myapi.onrender.com/api), I get an error 404. I used this tutorial to deploy my api : https:// docs.render.com/deploy-php-laravel-docker and I'm using postman to make the requests. Does someone know how to fix this ? Thanks

0 likes
13 replies
Jsanwo64's avatar

What error are you getting in Log? Also did you apply

php artisan optimize:clear
Yahrod's avatar
Level 1

Nothing in the log except this " ::1 - - [26/Sep/2024:21:07:46 +0000] "GET /api HTTP/1.1" 404 485 "-" "PostmanRuntime/7.42.0" "

Yes I did, in the "00-laravel-deploy.sh" file

DEV_BACKEND's avatar

Hello, I just encountered the same problem. Were you able to find the solution? Need help!

DEV_BACKEND's avatar

@Yahrod Hello, I just encountered the same problem. Were you able to find the solution? Need help!

Yahrod's avatar
Level 1

So I did some digging. Apparently the issue is when I'm adding something behind the Render url... This "https: //myapi.onrender.com/" works but this "https: //myapi.onrender.com/api" does not.

So I told my self "maybe the routes are not created" so I added this commad "php artisan route:list" and here is the output :

POST api/products ...... products.store › ProductController@store

GET|HEAD api/products ...... products.index › ProductController@index

GET|HEAD api ............................ generated::lnwA0mh8RHztYh4y

As you can see, the routes are created. So I think the the issue is comming from the "nginx-site.conf" file :

But I don't know where... Does someone sees what is wrong ?

1 like
Jsanwo64's avatar

It sounds like you're running into issues with routing when accessing your Laravel API on Render. Since the root URL works, but /api results in a 404 error, there are a few things to check and adjust:

Your Nginx configuration looks mostly correct, but ensure that the handling of routes and requests is correctly set up for Laravel. Here's a refined version of your nginx-site.conf configuration to help with Laravel routing:

Environment Configuration

Make sure your environment variables are correctly set on Render. Specifically, check your APP_URL in your .env file to ensure it matches your Render URL:

APP_URL=https://myapi.onrender.com

Testing Routes

Test your API routes using Postman with the complete URL (like https://myapi.onrender.com/api/products). Make sure that the routes listed in php artisan route:list are correctly prefixed with /api.

Yahrod's avatar
Level 1

@jsanwo64 Hello and thank you for the answer !

I try to fix my file with yours suggestions but still the same issue... About the environment variables, here is what i have in Render :

  • APP_KEY : generated by php artisan key:generate --show same in my .env file
  • DATABASE_URL : the Internal Database URL given by my Render pg database
  • DB_CONNECTION : pgsql

Is something missing ? Should I use only my .env file ?

Jsanwo64's avatar

@Yahrod try that. Also, make sure the following database credentials match what's set in Render's environment:

DB_CONNECTION=pgsql
DB_HOST=<Render PG Host>
DB_PORT=5432
DB_DATABASE=<Database Name>
DB_USERNAME=<Database Username>
DB_PASSWORD=<Database Password>

APP_URL: As mentioned earlier, ensure that APP_URL is set correctly both in the .env file and in Render's environment variables:

APP_URL=https://myapi.onrender.com

CORS Configuration: Another potential issue could be CORS (Cross-Origin Resource Sharing). If you're using APIs from different clients, ensure your app allows requests from outside origins. You can configure this in config/cors.php. For testing, you can set the allowed_origins as follows:

'paths' => ['api/*'],
'allowed_origins' => ['*'],

then clear config cache

php artisan config:cache
Yahrod's avatar
Level 1

@jsanwo64 So I tried to add the cors file but it gives me a new error :

In LoadConfiguration.php line 99:                                                             
array_merge(): Argument #2 must be of type array, int given

The database seems correct because the migration works

Jsanwo64's avatar

@Yahrod Check config/cors.php File: Make sure that the CORS configuration file does not contain any unexpected values or types. Here is a standard format for the config/cors.php file:

return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => false,
];
Jsanwo64's avatar

can you post what you have in your nginx-site.conf, RouteServiceProvider.php, .env and config/cors.php

Yahrod's avatar
Level 1

nginx-site.conf :

.env :

config/cors.php :

'paths' => ['api/*'],
'allowed_origins' => ['*'],

RouteServiceProvider.php :

Jsanwo64's avatar

lets try something.

  1. In your Nginx Configuration (nginx-site.conf), add some specific handling for the /api routes to avoid conflicts or unexpected behavior.
 # Handle API requests
    location /api {
        try_files $uri $uri/ /index.php?$query_string;
    }

So it will look like this

  1. Make sure the routes are defined properly in routes/api.php. If your routes are under a prefix, they should be accessible with the /api path.
Route::get('/products', [ProductController::class, 'index'])->name('products.index');
  1. Check Logs

Since you’re not seeing any helpful output in the logs, you may want to add some debugging or logging in your Laravel application. This can help to identify if the request is reaching the Laravel application at all.

In your routes/api.php, you can add temporary logging to check if the route is hit:
Route::get('/products', function () {
    \Log::info('Products route hit');
    return response()->json(['message' => 'Hello from products!']);
});

Please or to participate in this conversation.