4

At work we have a single staging server with a staging domain, something like https://staging.example.com. We recently decided to switch from Apache to NGINX on a new server and we're having issues with our Laravel routing.

All of our laravel apps sit in sub-directories on the staging server, like so.

I've tried configuring the NGINX conf file as specified in the Laravel docs but get a 404 when accessing any 2nd level route, i.e. https://staging.example.com/app1/public/a/b

Using something like the below config, I can access all the routes in an app.

location @laravel {
        rewrite /app1/public/(.*)$ /app1/public/index.php?$1;
}

location / {
        try_files $uri $uri/ @laravel;
}

However, we have many apps hosted on this server and we don't want to have to update an NGINX conf file every time we want to add an app to the server.

Is there a way of constructing a rewrite to apply to any sub-directory and keep Laravel's routing system working?

Note: I've also tried this rewrite rewrite (.*)/(.*)$ $1/index.php?$2 and that doesn't work for 2nd level routes.

1 Answer 1

1

Your first capture is probably too greedy, you should limit it by using:

rewrite ^(/[^/]+/[^/]+)/(.*)$ $1/index.php?$2 last;

See this useful resource on regular expressions.

Sign up to request clarification or add additional context in comments.

2 Comments

Your last solution with placing the public part into the rewrite worked for me, but the other two, more dynamic solutions, did not work. I'll keep tinkering with the solution you have so I don't have to hard-code the public path into the rewrite. Thanks!
I have updated the answer to accept two path elements in the first capture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.