我想要將大型檔案上傳到 AWS Elastic Beanstalk 環境,而不會收到錯誤訊息「413 請求實體太大」。
簡短描述
依預設,NGINX 在檔案上傳時有不能超過 1MB 的大小限制。如果請求的大小超過設定的值,則會傳回 413 請求實體太大錯誤。若要上傳大於 1 MB 的檔案,請在 NGINX 組態檔案中設定 client_max_body_size 指令。
**重要:**M 和 MB 是「megabyte」的同等表達式。例如,2M 等於 2 MB。但是,在組態檔案中只能使用 M,因為 MB 在組態檔案中無效。
解決方案
若要在 Amazon Linux 2 環境中設定 client_max_body_size,請執行以下操作:
1. 若要擴充 Elastic Beanstalk 預設 NGINX 組態,請新增包含以下項目的 .conf 組態檔案 client_max_body_size.conf:
client_max_body_size 50M;
備註:在上面的範例中,client_max_body_size 的值會更新為 50M。根據您的要求,以任何值來替代 50。
2. 將 .conf 組態檔案 client_max_body_size.conf 複製到應用程式來源服務包中名為 .platform/nginx/conf.d/ 的資料夾。Elastic Beanstalk NGINX 組態會自動在此資料夾中包含 .conf 檔案。如果來源服務包中不存在此路徑,請務必建立此路徑。以下範例顯示應用程式 zip 檔案中 .platform 目錄和 .conf 檔案的結構:
-- .ebextensions
-- other non nginx server config files
-- .platform
-- nginx
-- conf.d
-- client_max_body_size.conf
-- other application files
檔案 client_max_body_size.conf 具有的路徑如下所示:my-app/.platform/nginx/conf.d/client_max_body_size.conf。
3. 將您的程式碼和新的 .platform/ 目錄一起部署為 Elastic Beanstalk 環境中的新應用程式版本。
4. 部署完成後,登入 Elastic Beanstalk 環境上執行的執行個體。登入後,檢查設定是否已套用至 NGINX 伺服器。若要執行此操作,請使用以下命令:
$ sudo nginx -T | egrep -i "client_max_body_size"
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
client_max_body_size 50M;