如何解決在將檔案上傳到 Elastic Beanstalk 環境時出現的 “413 Request Entity Too Large” 錯誤?

1 分的閱讀內容
0

我想在我的 NGINX 組態中自訂 "client_max_body_size" 值,以便我可以將大型檔案上傳到 AWS Elastic Beanstalk 環境。

解決方法

NGINX 預設檔案上傳限制為 1 MB。若要上傳大於 1 MB 的檔案,請更新 client_max_body_size 值。

**重要事項:**M 和 MB 是 "megabyte" 的相等表示式。例如,2M 等於 2 MB。但是,NGINX 組態只接受 "M" 作為有效的語法值。數字和字母 "M" 之間沒有空格。

注意: 若要為所有 Amazon Linux 2 和 Amazon Linux 2023 平台版本自訂預設代理組態,請參閱擴展 Elastic Beanstalk Linux 平台

若要在 Amazon Linux 2 和 Amazon Linux 2023 中設定 client_max_body_size 值,請完成下列步驟:

  1. 建立名為 client_max_body_size.conf 的 .conf 組態檔,其中包含以下內容:

    client_max_body_size 50M;
  2. 將 .conf 檔案 client_max_body_size.conf 複製到應用程式來源套件中名為 .platform/nginx/conf.d/ 的資料夾中。

    注意: Elastic Beanstalk NGINX 組態會自動包含 .platform 資料夾中的 .conf 檔案。如果您的來源套件中沒有 .conf 檔案,請確定建立此路徑。

下列範例顯示 Java SE 平台上應用程式 zip 檔案中 .platform 目錄和 .conf 檔案的結構。.platform 資料夾必須放置於所有 Linux 平台的應用程式來源套件的根目錄。如需綁定應用程式的詳細資訊,請參閱平台的說明文件。

~/my-app.zip/
|-- web.jar
|-- Procfile
|-- readme.md  
`-- .platform/
    |-- nginx/                # Proxy configuration
    |   `-- conf.d/
    |       `-- client_max_body_size.conf
  1. 在 Elastic Beanstalk 環境中部署新的應用程式版本。
  2. 使用 SSH 登入在 Elastic Beanstalk 環境中執行的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體。
  3. 若要驗證 NGINX 組態語法是否正確,以及對 client_max_body_size 值所做的變更是否已套用,請執行下列命令。
    $ 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;

相關資訊

建立應用程式來源套件

AWS 官方
AWS 官方已更新 1 年前