0

I've got an existing WordPress site and I need to get a Laravel app to work in a sub-folder called 'api'. This is an nginx site, so .htaccess redirects will not work, and the best solution if it needs a redirect would be a PHP solution as I'm not sure I'll be able to access the nginx config directly on this particular server. I'm able to access the index.php file in the /public/ folder of the Laravel app, but going to /api/route/ takes me to a WordPress 404 page. I tried doing redirects in nginx config and PHP but nothing seems to be working. Is there something specific I need to do for putting a Laravel app in a sub-folder? I've inherited the project from another person and it is currently working where it is but it needs to be moved to a new server.

My routes look like this:

Route::group(array('before' => 'api_auth'), function()
{
    Route::get('/', 'Home\HomeController@index');
    Route::resource('cusomter', 'Customer\CustomerController', array('only' => array('show', 'store')));
    Route::resource('customer.conversion', 'Customer\CustomerController', array('only' => array('index')));
    Route::resource('customer.search', 'Customer\CustomerController', array('only' => array('index')));

});
primetimejas
  • 231
  • 2
  • 5

1 Answers1

0

One way is to have Nginx try to access Wordpress first, but then to try Laravel next if it doesn't hit, using try_files. This is how I do it when I have Cake PHP on the root of the domain and Wordpress in the /blog directory. There are probably downsides to this approach, but it works fine on my sites - low volume.

location / {
  try_files $uri $uri/ /api/route/index.php?$args;
}

Another way could be to define locations for each app. Pretty sure my regular expressions are wrong and wouldn't be surprised if something else isn't right as I haven't tested it, but this should be conceptually ok and lead you in the right direction.

server {
  root /var/www/html;

  location ~ /api/route {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME /api/route/$fastcgi_script_name; 
  }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

Happy to be corrected or my answer expanded on.

Tim
  • 30,383
  • 6
  • 47
  • 77
  • Please expand - I'm struggling to understand how your single location block routes to 2 different front controllers. Also, why are you using a regex match with the 2 location example when it only needs a prefix based match? – symcbean Dec 03 '20 at 01:02
  • Ah - reading the 2 locations config - you are not using a front controller for the Cake stuff. I don't think its possible within a try_files directive but assuming that it might be, any solution would be significantly less transparent than using 2 location blocks. – symcbean Dec 03 '20 at 01:06
  • This is a four year old question. If you need multiple locations, add multiple locations. – Tim Dec 03 '20 at 02:30