問題描述
在PHP項(xiàng)目部署在App Service后熊楼,上傳文件如果大于1MB就會(huì)遇見 413 Request Entity Too Large 的問題霹娄。
問題解決
目前這個(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ù)器。然后請求才會(huì)傳遞到PHP應(yīng)用中执泰。
基于以上分析枕磁,在PHP應(yīng)用中,會(huì)收到Nginx 和PHP雙重限制术吝。所以傳遞文件的限制問題設(shè)計(jì)到兩個(gè)方面:
一:Nginx 服務(wù)器對上傳文件大小的限制计济。(默認(rèn)限制為 1 MB,需通過 client_max_body_size 參數(shù)修改大信挪浴)
二:PHP 對上傳文件文件大小的限制沦寂。(默認(rèn)限制為 2MB,可以通過 upload_max_filesize 和 post_max_size 修改大刑匝谩)
所以传藏,本文主要介紹,如何在App Service For Linux環(huán)境中,修改Nginx對文件大小的限制和PHP文件大小的限制毯侦。
第一部分:修改 Nginx client_max_body_size
第一步:進(jìn)入App Service SSH 頁面哭靖,尋找Nginx的default文件,路徑為( /etc/nginx/sites-available/default)叫惊,通過CAT查看默認(rèn)的內(nèi)容款青。
上圖中執(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
如果通過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)命令中,以便長久有效瘪弓。如果啟動(dòng)命令中內(nèi)容過多垫蛆,可以把全部內(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
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中
或者也可以直接在 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
第二步:回到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文件的路徑而改變
第三步: 通過phpinfo()函數(shù)驗(yàn)證修改后的參數(shù)
在info.php文件中寫入 <?php phpinfo(); ,然后通過url訪問 https://<sitename>.chinacloudsites.cn/info.php
echo "<?php phpinfo();" >> info.php
附錄:PHP上傳文件的可用代碼
W3School PHP ****File Upload : https://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)之徐生。 云中柠并,恰是如此!
標(biāo)簽: App Service, App Service for Linux, Azure 環(huán)境, PHP+Nginx 413, 413 Request Entity Too Large