I trying to migrate my backend website (containing API service to mobile apps) from LAMP into docker platform. currently this docker split into 3 parts (Laravel App, database & nginx). sofar, the website launch successfully without error.
However, I need the base URL to be like the below:
http://backend.example.com/public/
so, if I want to login, URL will be http://backend.example.com/public/login
, also API URL with above format like http://backend.example.com/public/api/v1
What I have tried:
- Set
APP_URL
value in.env
tohttp://backend.example.com/public/
- Set below setting in
config/app.php
to:
'url' => env('APP_URL', 'https://backend.example.com/public'),
'asset_url' => env('ASSET_URL', null)
- run
php artisan route:clear
andphp artisan migrate
but still not successful, everytime I launch web browser, URL still stuck to:
http://backend.example.com/
http://backend.example.com/login/
http://backend.example.com/api/v1/
etc
any idea to overcome above problem?
===Additional Note
- nginx conf for nginx docker:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
original base URL in LAMP are
http://backend.example.com/public/
, in original i'm using LAMP (Apache PHP MySQL) but in current Docker I'm using (Nginx MySQL php:7.4-fpm), however because an error, i change something so original base url cannot be achieved anymore...Reference of This Migration can be found here.
.htaccess
at apache (old) configuration:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
CodePudding user response:
The APP_URL doesn't really have anything to do with setting the base URL for the actual routes, it is only for setting the base URL that the route helpers append to. What you're looking for instead is a route prefix. One thing to note before we get into this code though YOU NEED TO BE SURE YOU ARE ALWAYS USING ROUTE HELPERS TO DEFINE ROUTES, FORM ACTIONS, BASICALLY ANYTHING THAT INTERACTS WITH A URL. Otherwise you will have to remember to add the public to the path everywhere.
Open up your Route Service Provider in app/Providers/RouteServiceProvider
look for the map() function and add the route prefix
Route::prefix('public')->group(function () {
$this->mapApiRoutes();
$this->mapWebRoutes();
});
that's it. Now any route that is mapped by the route service provider will have the public/ prefix applied.
My source for this is just an extrapolation of the documentation for how to add a prefix to any route group https://laravel.com/docs/9.x/routing#route-group-prefixes