Laravel - NGINX + Apache reverse proxy 404 on all routes Laravel - NGINX + Apache reverse proxy 404 on all routes apache apache

Laravel - NGINX + Apache reverse proxy 404 on all routes


So i think i've figured it out, if you have a similar problem, a possible solution is to add the $uri variable to the try_files directive

server {    listen 80;    server_name application.test www.application.test;    root /home/vagrant/application/public;    index index.php index.html;    location / {        try_files $uri $uri/ /index.php$uri;    }    location ~ \.php {        proxy_pass http://localhost:8080;        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_set_header X-Forwarded-Proto $scheme;    }    location ~ /\. {      deny all;    }}

That way, when nginx calls your index.php it'll call it like http://application.test/index.php/hello and this is the URL that Apache ultimately receives and processes through PHP.

So then, .htaccess comes into play, Laravel does it's thing and everything works, and this still only affects PHP files, your static files should still be served directly by NGINX.

If you find anything wrong with this approach please let me know.

UPDATE


A dumb mistake on my end later down the line lead me to believe something else was wrong, every route seemed to display the contents of my "/" root route.After trying to use Homestead instead of my custom setup i was surprised to find it affected Homestead as well, so it had to be related to my app itself and not the Server.

Basically it was the Exception handler returning the root view because my requests were missing some data required by my FormRequests, after changing my Exception handler class to handle JSON responses it worked as expected.

If you fall into this kind of trap, try to make sure your app works with Homestead, if it does then there's something wrong with your vagrant setup, if not, then it's your Laravel app that's to blame.

If your Exception Handler just does the typical render, then be wary of the fact it'll attempt to redirect to the previous route, which if you're developing an API with Postman will be "/".