I have to laravel app on the same server. On is consuming the api of the second. To avoid latency, i would like to acess the api app on localhost from my main app like : http://localhost/api. The api app will not be publicy accessible.
How does the nginx configuration looks like i this case ?
You can use php artisan serve, valet or homestead... all of these serve from your machine. localhost is simply an alias to 127.0.0.1, so when you see that in these apps with a port number... it is all still serving from your localhost.
You're asking about nginx config makes me think you want to access from localhost on a web server. That is not going to reduce latency... only throw a bunch of errors if you aim localhost to another server. I would not advise that.
The main problem here is that if you're serving multiple apps/apis/sites via nginx on the same host, on the same port the way nginx determines which vhost config to use is determined by the hostname.
You could potentially pass a custom header and have nginx also look at that to determine the vhost to use, or run that other backend on a different port in which case your main app would hit localhost:[port_number]
If that api is also supposed to be directly accessible by other applications not just your own, then you'll want it to also be accessible remotely via hostname too.
The easiest option probably would be to create a second vhost for your api the listens on a different port, not open through the firewall, with servername 'localhost', and then point at it from your other app to consume it via the internal network on the server.