国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

PHP-FPM與Nginx通信報(bào) 502 Bad Gateway或504 Gateway Timeout終極解決方案(適用于PHP執(zhí)行耗時(shí)任務(wù)情況下的報(bào)錯(cuò))

這篇具有很好參考價(jià)值的文章主要介紹了PHP-FPM與Nginx通信報(bào) 502 Bad Gateway或504 Gateway Timeout終極解決方案(適用于PHP執(zhí)行耗時(shí)任務(wù)情況下的報(bào)錯(cuò))。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

前置條件:

適用于常規(guī)請求都沒問題,但是執(zhí)行某些php腳本需要超過一分鐘的情況下的502/504,并不是任何請求都502/504的情況(這說明php-fpm或者nginx配置錯(cuò)誤)。

出現(xiàn)502/504的原因

502

執(zhí)行腳本時(shí)間太長,期間php沒有返回任何的數(shù)據(jù)。php-fpm超時(shí),nginx沒超時(shí)。nginx認(rèn)為php-fpm罷工了,然后拋出了異常。

504

執(zhí)行腳本時(shí)間太長,期間php沒有返回任何的數(shù)據(jù)。php-fpm沒超時(shí),nginx超時(shí)。nginx認(rèn)為php-fpm響應(yīng)太慢,nginx沒憋住拋出了異常。

不生效的解決方案(防止各位師傅踩坑):

代碼

set_time_limit(0);
ignore_user_abort(true);
ini_set('max_execution_time', 600);

不生效原理剖析

以上代碼的作用設(shè)置了php代碼本身可以更長的時(shí)間處理任務(wù)并且不報(bào)致命錯(cuò)誤,但不代表程序一定無限制的可以執(zhí)行這么久。因?yàn)镹ginx與PHP進(jìn)程通信方式是檢測到.php的文件交給php-fpm進(jìn)程處理,php-fpm是一個(gè)fastcgi進(jìn)程管理器,php-fpm一旦超時(shí),php-fpm會強(qiáng)制終結(jié)掉這個(gè)進(jìn)程,這就是報(bào)502的原因。
這段代碼又無法控制nginx fastcgi的一些機(jī)制,所以報(bào)504的原因。
意味著僅代碼層配置還不夠,服務(wù)器也得配置。

官方文檔對這2個(gè)函數(shù)和1個(gè)配置的解釋:

##### set_time_limit:
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

##### ignore_user_abort:
Set whether a client disconnect should abort script execution.When running PHP as a command line script, and the script's tty goes away without the script being terminated then the script will die the next time it tries to write anything, unless enable is set to true.

##### max_execution_time:
This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.
On non Windows systems, the maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.
**Your web server can have other timeout configurations that may also interrupt PHP execution..Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.**
加粗字體意思是說:Web 服務(wù)器可以有其它超時(shí)配置,這些配置也可能會中斷 PHP 執(zhí)行。Apache 有一個(gè) Timeout 指令,IIS 有一個(gè) CGI 超時(shí)功能。 兩者都默認(rèn)為 300 秒。(nginx也有一個(gè)fastcgi超時(shí)配置,默認(rèn)60秒)。

502解決方案

再php-fpm.conf中添加request_terminate_timeout = 600即可,如下:

#編輯php-fpm配置
vim /usr/local/php/etc/php-fpm.conf
#添加此配置,單位默認(rèn)為秒,多少秒請根據(jù)情況自行設(shè)定
request_terminate_timeout = 600
#保存后重啟
service php-fpm restart

504解決方案

再nginx配置中添加 fastcgi_connect_timeout 600; fastcgi_read_timeout 600; fastcgi_send_timeout 600; 即可,如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-711080.html

#編輯nginx某個(gè)站點(diǎn)的配置
vim /usr/local/nginx/conf/vhost/test.conf
#再location中添加以下配置,單位默認(rèn)為秒,多少秒請根據(jù)情況自行設(shè)定,完整代碼塊如下:
location ~ \.php$ {
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
	include        /usr/local/nginx/conf/fastcgi_params;
	fastcgi_connect_timeout 600; 
	fastcgi_read_timeout 600; 
	fastcgi_send_timeout 600; 
}

#保存后測試配置是否有問題,如果有問題,請修改好后再次嘗試。
../../sbin/nginx -t
#確認(rèn)配置正常,重啟
service nginx restart

配置含義官方說明:

PHP-FPM:
request_terminate_timeout
The timeout for serving a single request after which the worker process will be killed. This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason. A value of '0' means 'Off'. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.


Nginx:
fastcgi_connect_timeout 60s;
Defines a timeout for establishing a connection with a FastCGI server. It should be noted that this timeout cannot usually exceed 75 seconds.

fastcgi_read_timeout 60s;
Defines a timeout for reading a response from the FastCGI server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the FastCGI server does not transmit anything within this time, the connection is closed.

fastcgi_send_timeout 60s;
Sets a timeout for transmitting a request to the FastCGI server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the FastCGI server does not receive anything within this time, the connection is closed.

到了這里,關(guān)于PHP-FPM與Nginx通信報(bào) 502 Bad Gateway或504 Gateway Timeout終極解決方案(適用于PHP執(zhí)行耗時(shí)任務(wù)情況下的報(bào)錯(cuò))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 解決504 GATEWAY TIMEOUT Nginx網(wǎng)關(guān)超時(shí)

    解決504 GATEWAY TIMEOUT Nginx網(wǎng)關(guān)超時(shí)

    最近遇到一個(gè)問題504 GATEWAY TIMEOUT的問題,在瀏覽器的NetWork里面看是這個(gè)效果。時(shí)間大概是60s。 目前后端技術(shù)架構(gòu)主要是nginx和php-fpm,前端主要是vue框架打包發(fā)布。 于是首先想到了是nginx超時(shí)時(shí)間或者與php的超時(shí)時(shí)間設(shè)置的過段,然后配置nginx.conf設(shè)置了這些參數(shù)。 然后發(fā)現(xiàn)

    2024年02月02日
    瀏覽(92)
  • 【面試題28】什么是PHP-FPM?它與PHP和Nginx有什么關(guān)系

    【面試題28】什么是PHP-FPM?它與PHP和Nginx有什么關(guān)系

    本文已收錄于PHP全棧系列專欄:PHP面試專區(qū)。 計(jì)劃將全覆蓋PHP開發(fā)領(lǐng)域所有的面試題, 對標(biāo)資深工程師/架構(gòu)師序列 ,歡迎大家提前關(guān)注鎖定。 PHP-FPM(FastCGI Process Manager)是PHP的FastCGI進(jìn)程管理器,它是PHP 5.3.3及更高版本的一部分。它通過為每個(gè)請求分配一個(gè)獨(dú)立的進(jìn)程來提

    2024年02月13日
    瀏覽(34)
  • PHP+Nginx經(jīng)常出現(xiàn)502、504原因與解決方法

    PHP+Nginx經(jīng)常出現(xiàn)502、504原因與解決方法

    很多新手剛開始做網(wǎng)站可能感覺不到502,504的問題,當(dāng)?shù)饶憔W(wǎng)站到達(dá)了一定水平的時(shí)候,流量起來的時(shí)候,你會發(fā)現(xiàn)經(jīng)常會遇到502、504類似的問題。 502 Bad Gateway:作為網(wǎng)關(guān)或者代理工作的服務(wù)器嘗試執(zhí)行請求時(shí),從上游服務(wù)器接收到無效的響應(yīng)。 將請求提交給網(wǎng)關(guān)如php-fpm執(zhí)

    2024年02月15日
    瀏覽(27)
  • Nginx錯(cuò)誤502 Bad Gateway

    使用Nginx配置的反向代理,瀏覽器訪問的時(shí)候出現(xiàn) “502 Bad Gateway” 錯(cuò)誤,檢查了一下后臺error文件,發(fā)現(xiàn)有類似下面的錯(cuò)誤 其中 “upstream sent too big header while reading response header from upstream” 說明可能是nginx代理的緩沖區(qū)不夠,因此需要調(diào)整一下緩沖區(qū)的配置,主要包括下面幾

    2024年02月19日
    瀏覽(25)
  • 在Linux上使用PHP-FPM與Nginx實(shí)現(xiàn)高效的HTTP處理

    在Linux上使用PHP-FPM與Nginx實(shí)現(xiàn)高效的HTTP處理

    當(dāng)談到高效的HTTP處理時(shí),PHP-FPM(FastCGI進(jìn)程管理器)與Nginx的結(jié)合是許多web開發(fā)者的首選。這種組合提供了出色的性能、可擴(kuò)展性和穩(wěn)定性,尤其適用于高流量的網(wǎng)站和應(yīng)用程序。 1. 為什么選擇PHP-FPM與Nginx? 性能優(yōu)化 :PHP-FPM通過進(jìn)程管理和緩存機(jī)制,顯著提高了PHP腳本的執(zhí)

    2024年01月17日
    瀏覽(24)
  • nginx反向代理502-Bad Gateway問題解決

    nginx反向代理502-Bad Gateway問題解決

    配置nginx反向代理時(shí)出現(xiàn)502 通過nginx -t檢查配置以成功 通過nginx -s reload重新加載 通過cat /var/log/nginx/error.log查看錯(cuò)誤日志發(fā)現(xiàn)錯(cuò)誤信息,這里的錯(cuò)誤信息是“connecting to upstream ”。這里懷疑是selinux拒絕nginx 轉(zhuǎn)發(fā) 8080端口。 關(guān)閉selinux重新測試 關(guān)閉后重新測試正常,可以通過反向

    2024年01月19日
    瀏覽(30)
  • 【已解決】nginx 502 Bad Gateway 問題排查

    【已解決】nginx 502 Bad Gateway 問題排查

    訪問網(wǎng)站或請求接口時(shí),出現(xiàn): 日志一般放在/var/log/nginx下面。 跑流水線的話一般部署日志在控制臺可以直接看到(我遇到的一次就是構(gòu)建包下載下來大小為0kb,md5校驗(yàn)也不通過) 源碼安裝的nginx配置文件一般在 /usr/local/nginx/conf/nginx.conf/ 不是源碼安裝的一般在 /etc/nginx/ngi

    2024年02月15日
    瀏覽(28)
  • Linux系統(tǒng)下配置Nginx使部分URL使用多套自定義的PHP-FPM配置

    1. Tcp默認(rèn)的9000端口通信: php-fpm配置:listen = 127.0.0.1 與nginx進(jìn)程通信:fastcgi_pass 127.0.0.1:9000; 優(yōu)點(diǎn): 使用網(wǎng)絡(luò)傳輸,可以跨服務(wù)器。 TCP通信有一些校驗(yàn)機(jī)制,具有更高的穩(wěn)定性。 缺點(diǎn): 性能略微比socket差。 2. Unix Socket(套接字)通信: php-fpm配置 :listen = /tmp/php-cgi.sock 與

    2024年02月08日
    瀏覽(21)
  • nginx反向代理502-Bad Gateway問題解決方法

    用nginx反向代理 localhost:80 域名到服務(wù)器 localhost:8080 端口服務(wù)時(shí),訪問出現(xiàn)502 bad gateway 原因分析: 1.查看8080端口服務(wù)啟動 2.查看錯(cuò)誤日志:error.log,以centos7.x為例: 192.168.10.202 - - [08/May/2023:20:53:43 +0800] \\\"GET /jenkinsx/ HTTP/1.1\\\" 502 3693 \\\"-\\\" \\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/53

    2024年02月06日
    瀏覽(35)
  • Nginx反向代理的一個(gè)算法API的接口調(diào)用超時(shí):504,GateWay Timeout,怎么破?

    服務(wù)端由第三方部署了一個(gè)基于 darknet (一個(gè)較為輕型的完全基于C與CUDA的開源深度學(xué)習(xí)框架)的識別算法服務(wù),通過 Flask 的 Web 服務(wù)對業(yè)務(wù)服務(wù)暴露 API 接口。作為測試,一開始是直接通過 python3 app.py 的命令行啟動的服務(wù),然后在 Nginx 處通過反向代理過來的。 可是在通過前

    2023年04月08日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包