【Azure 應(yīng)用服務(wù)】PHP應(yīng)用部署在App Service for Linux環(huán)境中,上傳文件大于1MB時(shí),遇見了413 Request Entity Too Large 錯(cuò)誤的解決方法

問題描述

在PHP項(xiàng)目部署在App Service后,上傳文件如果大于1MB就會(huì)遇見 413 Request Entity Too Large 的問題。


image.png

問題解決

目前這個(gè)問題,首先需要分析應(yīng)用所在的環(huán)境。 在App Service for Linux環(huán)境中,為PHP提供的運(yùn)行時(shí)當(dāng)前只有PHP 8.0, 并且 PHP 8.0 中使用的Nginx作為代理服務(wù)器。然后請(qǐng)求才會(huì)傳遞到PHP應(yīng)用中。

基于以上分析,在PHP應(yīng)用中,會(huì)收到Nginx 和PHP雙重限制。所以傳遞文件的限制問題設(shè)計(jì)到兩個(gè)方面:

一:Nginx 服務(wù)器對(duì)上傳文件大小的限制。(默認(rèn)限制為 1 MB,需通過 client_max_body_size 參數(shù)修改大小)

二:PHP 對(duì)上傳文件文件大小的限制。(默認(rèn)限制為 2MB,可以通過 upload_max_filesize 和 post_max_size 修改大?。?/p>

所以,本文主要介紹,如何在App Service For Linux環(huán)境中,修改Nginx對(duì)文件大小的限制和PHP文件大小的限制。

第一部分:修改 Nginx client_max_body_size

第一步:進(jìn)入App Service SSH 頁面,尋找Nginx的default文件,路徑為( /etc/nginx/sites-available/default),通過CAT查看默認(rèn)的內(nèi)容。

image.png

上圖中執(zhí)行的指令有:

cd ..

cd etc/nginx/sites-availabled

ls

cat default

第二步:復(fù)制default的內(nèi)容到本地自己的PHP項(xiàng)目文件中,添加client_max_body_size后,隨項(xiàng)目文件一起部署到 /home/site/wwwroot/目錄下

(PS: 目錄可以自定義修改,但必須注意,如果項(xiàng)目部署的根目錄不在wwwroot中,必須同步修改 nginx 的 default配置文件,避免應(yīng)用報(bào)錯(cuò)找不到源文件)

default的默認(rèn)內(nèi)容為

server {
    #proxy_cache cache;
        #proxy_cache_valid 200 1s;
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com; 

    location / {            
        index  index.php index.html index.htm hostingstart.html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504  /50x.html;
    location = /50x.html {
        root /html/;
    }

    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Add locations of phpmyadmin here.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_connect_timeout 300; 
        fastcgi_send_timeout 3600; 
        fastcgi_read_timeout 3600;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
}

需要在其中添加 client_max_body_size , 比如這里設(shè)置為10m ** (不要寫成 10MB,并且結(jié)尾要帶上分號(hào) ;**)

server {
    #proxy_cache cache;
    #proxy_cache_valid 200 1s;
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php index.html index.htm;
    server_name  lbphplinuxtest01.chinacloudsites.cn;
    client_max_body_size 10m;

   location / {
        try_files $uri $uri/ /index.php$is_args$query_string;
        index  index.php index.html index.htm hostingstart.html;
    }

   # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504  /50x.html;
    location = /50x.html {
        root /html/;
    }

    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

   # Add locations of phpmyadmin here.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 3600;
        fastcgi_read_timeout 3600;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
}

第三步:PHP應(yīng)用文件部署后,可以也SSH中通過cp 命令把修改后的default文件復(fù)制到 nginx sites-available default目錄下,并且重啟Nginx服務(wù)(service nginx restart)。此步為臨時(shí)性有效。

此步執(zhí)行的命令為

##復(fù)制 default 到nginx sites-available目錄中
cp /home/site/wwwroot/default /etc/nginx/sites-available/default
##重新加載 nginx 服務(wù)
service nginx reload 
image.png

如果通過SSH直接在應(yīng)用的容器中執(zhí)行以上命令,當(dāng)App Service站點(diǎn)重啟,應(yīng)用重新部署時(shí),以上修改都會(huì)丟失,Nginx配置會(huì)恢復(fù)默認(rèn)。所以需要下一步永久性操作。

PS: 修改完成后,可以通過 Nginx -T 來查看Nginx所使用的配置參數(shù)。以及可以在 /var/log/nginx中查看error.log日志

第四步:把第三步的 cp 命令和 restart命令設(shè)置在 App Service的啟動(dòng)命令中,以便長(zhǎng)久有效。如果啟動(dòng)命令中內(nèi)容過多,可以把全部?jī)?nèi)容寫入到一個(gè)腳本文件中(如 startscript.sh),并隨項(xiàng)目文件一起放置。然后把文件全路徑填入啟動(dòng)命令中。

把第三步的兩句命令用分號(hào)(;)連接在一起設(shè)置在Startup Command中,操作如下圖:

cp /home/site/wwwroot/default /etc/nginx/sites-available/default; service nginx reload

image.png

1)在App Service的目錄頁面中, 點(diǎn)擊Configuration目錄進(jìn)入Configuration頁面
2)選擇General Settings選項(xiàng)卡
3)在Startup Command中添加 cp和restart命令
4)點(diǎn)擊保存按鈕。應(yīng)用會(huì)自動(dòng)重啟并運(yùn)行命令
注意:當(dāng)以上操作完成后,Nginx上傳文件默認(rèn)1MB 的限制就變?yōu)榱?10MB,但是因?yàn)镻HP的限制還沒有修改,所以當(dāng)在應(yīng)用中上傳文件大于2MB時(shí)候,依舊會(huì)收到 413 或者 404 錯(cuò)誤。只是,此時(shí)錯(cuò)誤的根源為PHP限制。

第二部分:修改PHP upload_max_filesize 和 post_max_size

第一步:在項(xiàng)目文件根目錄中創(chuàng)建 extensions.ini 的文件,并在其中設(shè)置 upload_max_filesize 和 post_max_size 值為50M。然后把修改后的文件一起部署到 wwwroot中

image.png

或者也可以直接在 SSH中通過命令創(chuàng)建 extensions.ini 文件和內(nèi)容

cd site
mkdir ini
cd ini
echo "upload_max_filesize=50M"  >> extensions.ini 
echo "post_max_size=50M"  >> extensions.ini 
cat extensions.ini
ls
image.png

第二步:回到Azure App Service 門戶頁面,在Application Setting中添加 PHP_INI_SCAN_DIR 參數(shù),指定它的值為 /usr/local/etc/php/conf.d:/home/site/ini 或者 /usr/local/etc/php/conf.d:/home/site/wwwroot/ini

PS: 前一段 /usr/local/etc/php/conf.d 路徑固定,為PHP Runtime加載配置配置文件的路徑,而一部分 /home/site/ini 則需要根據(jù)第一步extensions.ini文件的路徑而改變


image.png

第三步: 通過phpinfo()函數(shù)驗(yàn)證修改后的參數(shù)

在info.php文件中寫入 <?php phpinfo(); ,然后通過url訪問 https://<sitename>.chinacloudsites.cn/info.php

echo "<?php phpinfo();" >> info.php
image.png

附錄:PHP上傳文件的可用代碼

W3School PHP ****File Uploadhttps://www.w3schools.com/php/php_file_upload.asp

**PHPFileUpload **: https://github.com/zgcwkjOpenProject/PHPFileUpload

參考資料

NGINX Rewrite Rules for Azure App Service Linux PHP 8.x:https://azureossd.github.io/2021/09/02/php-8-rewrite-rule/index.html

Azure App Service Linux - Update PHP Settings:https://azureossd.github.io/2019/01/29/azure-app-service-linux-update-php-settings/

[END]

當(dāng)在復(fù)雜的環(huán)境中面臨問題,格物之道需:濁而靜之徐清,安以動(dòng)之徐生。 云中,恰是如此!

分類: 【Azure 應(yīng)用服務(wù)】

標(biāo)簽: App Service, App Service for Linux, Azure 環(huán)境, PHP+Nginx 413, 413 Request Entity Too Large

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容