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

Stato74's avatar

Docker, Ngnix and PHP 8.2 unable to find api route, gives a 404

I have a simple docker compose file that pulls in Ngnix and PHP 8.2 (both configs below) the process copies my laravel application from the src folder to the /var/www/html folder

When I run the docker container I can see the Laravel home page via http://localhost:8090 but when i use Postman going to http://localhost:8090/api/myendpoint I get a 404

In docker I get the following message

2023-05-30 11:38:35 devdocker-web-1 | 2023/05/30 10:38:35 [error] 31#31: *4 open() "/var/www/html/public/api/myendpoint" failed (2: No such file or directory), client: 172.20.0.1, server: phpfpm.local, request: "POST /api/myendpoint HTTP/1.1", host: "127.0.0.1:8090

It looks like its trying to find /var/www/html/public/api/myendpoint, which i know doesn't exist as the api is added because its an api route.

Any ideas on how to fix this ?

docker-compose.yml

version: '3.9'

services: web: image: nginx:latest ports: - '8090:80' volumes: - ./src:/var/www/html - ./default.conf:/etc/nginx/conf.d/default.conf links: - php-fpm php-fpm: image: php:8-fpm volumes: - ./src:/var/www/html

default.conf

server { index index.php index.html; server_name phpfpm.local; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /var/www/html/public; location ~ .php$ { try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass php-fpm:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } }

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message suggests that Nginx is looking for the API route in the wrong directory. The Laravel application's public directory should be the root directory for Nginx. To fix this, update the Nginx configuration file to set the root directory to /var/www/html/public instead of /var/www/html.

Here's the updated default.conf file:

server {
    index index.php index.html;
    server_name phpfpm.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public; # updated root directory
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

After updating the configuration file, rebuild the Docker containers and try accessing the API route again.

1 like
Stato74's avatar

Thanks for your help.

It was the following line I was missing

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

Glad is was simple config issue as I was looking networking and routing.

Much appreciated.

Please or to participate in this conversation.