# Nginx 開水管流量, Larave 檔案上傳問題 2022-12-28 ###### tags: `Nginx` `Laravel` `檔案上傳流量` `檔案上傳` `MobaXterm` `Web server` `Back end` `後端心得` --- 假如 在 Laravel 有使用 `php artisan make:controller imgUploadController` 製造一支專門來控制圖片上傳的 Controller 但如果有設定到 `file.max` 然後上傳時會顯示 HTTP error 413 問題, 這時候要開啟 nginx 的流量 ``` class imgUploadController extends Controller { public function imgUpload(Request $request) { // $fileName = $request->file('file')->getClientOriginalName(); // $imgpath = $request->file('file')->storeAs('imgUpload', $fileName, 'public'); // return response()->json(['location' => "/storage/$imgpath"]); $messages = [ 'file.max' => '圖片檔案不能大於 2048 kb', ]; $validator = Validator::make($request->all(), [ 'file' => 'max: 2048', ], $messages); if ($validator->fails()) { return response()->json([ 'error' => $validator->errors()->all(), ], 200, [], JSON_UNESCAPED_UNICODE); } $file = $request->file('file'); $path = $file->store('public/imgUpload'); $path = str_replace('public', '', $path); return response()->json(['location' => "/storage{$path}"]); /*$imgpath = request()->file('file')->store('imgUpload', 'public'); return response()->json(['location' => "/storage/$imgpath"]);*/ } public function deleteUpload(Request $request) { $fileName = $request->fileName; if (Storage::disk('public/imgUpload')->exists($fileName)) { Storage::disk('public/imgUpload')->delete($fileName); } return response()->json(['success' => '刪除成功']); } ``` --- ## 開啟圖片/檔案上傳流量限制問題 可參考: https://serverfault.com/questions/611239/increase-php-fpms-max-upload-post-size 以及 去開啟檔案大小上傳限制 在虛擬機上找 ``` cd nginx/conf.d/ ``` 並且編輯 ``` vi nginx.conf ``` 將 body 變肥一點(如果要限制流量攻擊), 可以參考前輩寫的: https://ithelp.ithome.com.tw/articles/10271702?sc=rss.iron ``` http { .... # 請求的 body 最大就是 100M client_max_body_size 100M; # 註: 通常不會讓 body 弄到 100M, 這樣太危險~ } ``` 然後在各專案的 conf 檔, 例如 `xxx.conf` 可以設定個別圖片, 影片的流量限制: ``` server { # 上傳圖片最高可以到 5MB location public/imgUpload { client_max_body_size 5M; proxy_pass http://後端位址; } } ``` ---- ## 接著去找 `php.ini` 使用指令 `/` 去找 ``` 這兩個設定一樣大比較好 /upload_max_filesize /post_max_size ``` 如果不擔心 body 肥大的問題的話, 檔案上傳限制調高 ↓ ![](https://i.imgur.com/Xyr7GMc.png) ![](https://i.imgur.com/tgbtoqC.png) 記得重啟~ ``` sudo systemctl restart php-fpm sudo systemctl restart nginx ``` 可以參考: [How to Validate on Max File Size in Laravel?](https://stackoverflow.com/questions/37432641/how-to-validate-on-max-file-size-in-laravel) https://serverfault.com/questions/611239/increase-php-fpms-max-upload-post-size https://stackoverflow.com/questions/58652896/laravel-file-does-not-exist-or-is-not-readable [status of 413 (Payload Too Large) in Laravel](https://stackoverflow.com/questions/70158235/status-of-413-payload-too-large-in-laravel) [Laravel API overriding default 413 Payload Too Large response](https://stackoverflow.com/questions/67258268/laravel-api-overriding-default-413-payload-too-large-response) [Laravel - validate file size when PHP max upload size limit is exceeded [duplicate]](https://stackoverflow.com/questions/46067336/laravel-validate-file-size-when-php-max-upload-size-limit-is-exceeded) --- 成功的話