欢迎来到日本小春精选,了解日本先从@日本生活基础课程开始!
欢迎 欢迎     登录 | 注册      消息
当前位置: 首页小组技术交流Nginx 502 Bad Gateway 自动重启脚本

[Linux]Nginx 502 Bad Gateway 自动重启脚本

<p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">Nginx 502 Bad Gateway的含义是请求的php-cgi已经执行,但是由于某种原因没有执行完毕而导致php-cgi进程终止。一般并发数太高站很容易出现此错误,不过修改下面的参数,搞个重启脚本就能解决。</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">打开 /usr/local/php/etc/php-fpm.conf 文件,修改如下几个参数:</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">&lt;value name=”max_children”&gt;5&lt;/value&gt;<br />&lt;value name=”request_terminate_timeout”&gt;0s&lt;/value&gt;<br />&lt;value name=”process_control_timeout”&gt;5s&lt;/value&gt;</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">max_children表示php-cgi的处理进程,如果max_children设置的较小,处理速度比较慢,等待的时间也较长。如果长时间没有得到处理的请求就会出现504 Gateway Time-out错误。设置max_children也需要根据服务器的性能进行设定,增大进程数,内存占用也会相应增大,正常情况下每个php-cgi所耗费的内存在20M左右,这里我设置的是100。</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">request_terminate_timeout是指fast-cgi的执行脚本时间,0s的含义是让php-cgi一直执行下去而没有时间限制,当出现502 Bad Gateway的时候,这个502的状态将一直持续下去不会改变,只能手动重启VPS才能解决。这个值可以根据你服务器的性能进行设定,这里我设置的是8s。</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">下面,需要做个重启php-fpm的脚本。</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">cd /home<br />vi restart-php-fpm.sh</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">输入如下代码,这个代码就是重启php-fpm的脚本了,脚本中的http://www.jichuan.net记得改成自己的网站地址:</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">#!/usr/bin/php<br />&lt;?<br />$url = ’http://www.jichuan.net&amp;#8217;;<br />$cmd = ’/usr/local/php/sbin/php-fpm restart’;<br />for($i = 0; $i &lt; 5; $i ++){<br />$exec = ”curl-connect-timeout 3 -i $url 2&gt;/dev/null”;<br />$res = shell_exec($exec);<br />if(stripos($res, ’502 Bad Gateway’) !== false){<br />shell_exec($cmd);<br />exit();<br />}<br />}<br />?&gt;</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">然后设置Crontab.<br />先设置restart-php-fpm.sh的权限为755</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">chmod 755 /home/restart-php-fpm.sh</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">运行crontab -e</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">crontab -e</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">设置restart-php-fpm.sh自动执行</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">* * * * * /home/restart-php-fpm.sh</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">查看设置成功还是失败</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">crontab -l</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">退出</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">exit 0</p><p style="font-size: 1.05em; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(213, 214, 215); ">注意:设置restart-php-fpm.sh自动执行可能会提示如下错误,<br />E486:pattern not found:home<br />E348: no string under cursor<br />你需要注意是否将vi切换到文本输入模式 也就是看ssh 客户端左下角是<br />否显示 –INSERT–<br />最好直接输入 * * * * * /home/restart-php-fpm.phps 而不是粘贴过去.<br />Vi 切换文本 方法 vi 提供了两个插入命令:i 和I<br />按Esc退出编辑模式,然后输入:x保存退出</p><br />
2012-10-31 16:44:55 来自:球球
用户评论(0)
正序阅读
你的回应
登录 | 注册