Reputation: 830
I'm currently trying to install a Laravel app in a sub-directory of a site using nginx to direct any traffic to that app.
I have followed the suggestions in the following Stackoverflow question, and it works perfectly Config nginx for Laravel In a subfolder
However, when using this method it removes the need for the sub-directory to be in the Laravel apps routes.
An example.
Say I am pointing all requests to https://example.com/shop to a /shop subdirectory using the following nginx entry...
location /shop {
alias /var/www/shop/public;
try_files $uri $uri/ @shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location @shop {
rewrite /shop/(.*)$ /shop/index.php last;
}
As stated, this works but the route for the shops "homepage" in the app will actually be
Route::get('/', ShopController::class)->name('shop.home');
Whereas I need it to be
Route::get('/shop', ShopController::class)->name('shop.home');
I don't have control over all the routing etc due to it being in packages, and there are various other reasons why this would be preferable for me anyway.
Is there a way to adapt the above nginx entry to achieve this? I have tried numerous things but can not seem to get it to work.
Thanks in advance.
Upvotes: 0
Views: 1617
Reputation: 1498
You need to set dynamicaly and change the fastcgi_param REQUEST_URI $request_url.
Basically to remove or add (depend on your needs) the word /shop from the REQUEST_URI
You may need to remove REQUEST_URI from fastcgi_params file.
More info and example can found in
https://serverfault.com/questions/697569/rewrite-url-with-fastcgi-in-nginx
I didn't tested it so maybe some changes need to made in some lines or change the order of things...
but you can try to use something like
location /shop {
alias /var/www/shop/public;
try_files $uri $uri/ @shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REQUEST_URI /shop$request_uri;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location @shop {
rewrite /shop/(.*)$ /shop/index.php last;
}
Upvotes: 2
Reputation: 830
I got this working thanks to a clue from @shushu304
I just updated the REQUEST_URI using the following.
location /shop {
alias /var/www/shop/public;
try_files $uri $uri/ @shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_URI /shop$request_uri; <--------
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location @shop {
rewrite /shop/(.*)$ /shop/index.php last;
}
Upvotes: 0