我試圖將我的后端網站(包含 API 服務到移動應用程式)從 LAMP 遷移到 docker 平臺。目前這個 docker 分為 3 個部分(Laravel 應用程式、資料庫和 nginx)。至此,網站啟動成功,沒有錯誤。
但是,我需要基本 URL 如下所示:
http://backend.example.com/public/
所以,如果我想登錄,URL 將是http://backend.example.com/public/login,API URL 也是上面格式的,比如http://backend.example.com/public/api/v1
我試過的:
- 將
APP_URL值設定.env為http://backend.example.com/public/ - 將以下設定設定
config/app.php為:
'url' => env('APP_URL', 'https://backend.example.com/public'),
'asset_url' => env('ASSET_URL', null)
- 運行
php artisan route:clear和php artisan migrate
但仍然不成功,每次我啟動網路瀏覽器時,URL 仍然停留在:
http://backend.example.com/
http://backend.example.com/login/
http://backend.example.com/api/v1/
ETC
有什么想法可以克服上述問題嗎?
===補充說明
- nginx docker 的 nginx conf:
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;
}
}
LAMP 中的原始基本 URL 是
http://backend.example.com/public/,原來我使用的是 LAMP(Apache PHP MySQL),但在當前的 Docker 中我使用的是(Nginx MySQL php:7.4-fpm),但是因為一個錯誤,我改變了一些東西無法再獲得原始基本網址...可以在此處找到此遷移的參考。
.htaccess在 apache(舊)配置:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
uj5u.com熱心網友回復:
APP_URL 與設定實際路由的基本 URL 沒有任何關系,它僅用于設定路由助手附加到的基本 URL。您正在尋找的是路由前綴。在我們進入這段代碼之前需要注意的一件事,盡管你需要確保你總是使用路由助手來定義路由、表單操作,基本上任何與 URL 互動的東西。否則,您將不得不記住將公眾添加到任何地方的路徑中。
在 app/Providers/RouteServiceProvider 中打開你的 Route Service Provider
查找 map() 函式并添加路由前綴
Route::prefix('public')->group(function () {
$this->mapApiRoutes();
$this->mapWebRoutes();
});
就是這樣。現在,路由服務提供商映射的任何路由都將應用 public/ 前綴。
我對此的來源只是對如何向任何路由組添加前綴的檔案的推斷https://laravel.com/docs/9.x/routing#route-group-prefixes
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/460830.html
標籤:php 拉拉维尔 码头工人 nginx laravel-5
上一篇:SQLSTATE[23000]:完整性約束違規:1062鍵“user_username_unique”的重復條目“asdf”(SQL:更新“用戶”設定“用戶名”=asdf
下一篇:使用laravel上傳檔案
