Help with NGINX Configuration for PHP API
Asked by
recipe_revision
Hey everyone, I'm running into an issue with my PHP API setup on NGINX. I've created an API that works perfectly fine locally using the built-in PHP server (php -S), but when I deploy it with NGINX, I keep getting a 'File not found' error on every route I try to access. I've tried tweaking the try_files directive in my NGINX configuration to options like `$uri $uri / /api/index.php?$query_string` and `$uri $uri / /index.php?$query_string`, but the error isn't going away. Any insights or suggestions would be greatly appreciated!
Answers
It sounds like you're having a tough time! Could you share your entire NGINX config? That would give us more context and help identify any potential misconfigurations.
Reply to this answer
When you see 'File not found', it often means NGINX isn't forwarding the request to PHP-FPM correctly. Double-check that your fastcgi_param SCRIPT_FILENAME points to the correct path for /api/index.php. If you want, I can help you troubleshoot your full config.
Gobbler: Here's my full config: [https://imgur.com/a/wFSbV0U](https://imgur.com/a/wFSbV0U). The HTTP port is matching this as well.
Reply to this answer
Give this configuration a shot:
```nginx
server {
server_name yourdomain.com;
root /var/www/yourproject;
index index.php;
location /api {
try_files $uri /api/index.php$is_args$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # make sure to adjust version/socket
}
}
```
Make sure to tweak it according to your setup!
flat-pack-desk: Unfortunately, I'm still getting 'file not found'. When I try accessing site.com/api, it doesn't work.
Reply to this answer
Add your answer
Answers are reviewed before publication.
turnstile__operator: I would share it, but I'm using the recovery console on Digital Ocean and can't copy from nano.
Gobbler: Check this link for a screenshot of my config: [https://imgur.com/a/wFSbV0U](https://imgur.com/a/wFSbV0U)