0

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.

1
  • Doe's sub-domain can be ok for you? I posted some examples you can try ... but probably sub-domain shop.website.com will solve all of our issues :) Commented Jan 5, 2021 at 18:04

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, this did not work for me. It still points to / instead of /shop
0

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;
}

1 Comment

Cool, this is what i wanted to suggest also (after thinking on it over night :) ) ... any way I updated my example also and you are welcome to vote to my answer :)

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.