Home > other >  laravel - can not change base URL in Docker Laravel PHP Nginx
laravel - can not change base URL in Docker Laravel PHP Nginx

Time:04-14

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:

  1. Set APP_URL value in .env to http://backend.example.com/public/
  2. Set below setting in config/app.php to:
'url' => env('APP_URL', 'https://backend.example.com/public'),
'asset_url' => env('ASSET_URL', null)
  1. run php artisan route:clear and php 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

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

  2. Reference of This Migration can be found here.

  3. .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

  • Related