跳到主要内容
帮助中心

新闻公告

产品更新、运营公告与行业观察
一手消息,第一时间同步给你

修改Apache的超时设置,解决长连接请求超时问题

<article><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">某日,组内后台开发找到我,问我们的 WEB 服务器超时设置是多少。他反馈的问题是,有一个 VLAN 切换任务 cgi 接口经常返回 504 网关超时错误,要我分析解决下。<img src="/Uploads/2022-11-05/6365f28f96552.png" title="6365f28f96552.png" alt="image.png"/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">我问了一下,得知这个请求遇到网络设备对象较多的时候,需要小半个小时才能完成,也就是要用到长连接才行。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">老规矩,从开发那拿到接口地址,得到接入层服务器 IP,是一台 Haproxy 代理,看了一下 Haproxy 的超时设置:</p><pre class="lang:default decode:true hljs properties" style="box-sizing: border-box; font-size: 1em; font-family: &quot;Lucida Console&quot;, Monaco, &quot;Courier New&quot;; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">#&nbsp;设置成功连接到一台服务器的最长等待时间,默认单位是毫秒,新版本的&nbsp;haproxy&nbsp;使用&nbsp;timeout&nbsp;connect&nbsp;替代,该参数向后兼容contimeout&nbsp;3600#&nbsp;设置连接客户端发送数据时的成功连接最长等待时间,默认单位是毫秒,新版本&nbsp;haproxy&nbsp;使用&nbsp;timeout&nbsp;client&nbsp;替代。该参数向后兼容clitimeout&nbsp;3600#&nbsp;设置服务器端回应客户度数据发送的最长等待时间,默认单位是毫秒,新版本&nbsp;haproxy&nbsp;使用&nbsp;timeout&nbsp;server&nbsp;替代。该参数向后兼容srvtimeout&nbsp;3600</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">各种 1 小时超时,所以排除 Haproxy 的影响,继续往下看。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">Haproxy 代理的是 2 台 Apache,也就是部署了 cgi 接口的服务器。第一时间查看了 httpd.conf 和 httpd-vhost.conf 中的配置,居然没找到超时设置。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">于是,搜索了下相关教程,发现原来藏在了 httpd-default.conf 当中:</p><pre class="lang:default decode:true hljs properties" style="box-sizing: border-box; font-size: 1em; font-family: &quot;Lucida Console&quot;, Monaco, &quot;Courier New&quot;; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">##&nbsp;This&nbsp;configuration&nbsp;file&nbsp;reflects&nbsp;default&nbsp;settings&nbsp;for&nbsp;Apache&nbsp;HTTP&nbsp;Server.##&nbsp;You&nbsp;may&nbsp;change&nbsp;these,&nbsp;but&nbsp;chances&nbsp;are&nbsp;that&nbsp;you&nbsp;may&nbsp;not&nbsp;need&nbsp;to.###&nbsp;Timeout:&nbsp;The&nbsp;number&nbsp;of&nbsp;seconds&nbsp;before&nbsp;receives&nbsp;and&nbsp;sends&nbsp;time&nbsp;out.#Timeout&nbsp;300##&nbsp;KeepAlive:&nbsp;Whether&nbsp;or&nbsp;not&nbsp;to&nbsp;allow&nbsp;persistent&nbsp;connections&nbsp;(more&nbsp;than#&nbsp;one&nbsp;request&nbsp;per&nbsp;connection).&nbsp;Set&nbsp;to&nbsp;&quot;Off&quot;&nbsp;to&nbsp;deactivate.#KeepAlive&nbsp;On##&nbsp;MaxKeepAliveRequests:&nbsp;The&nbsp;maximum&nbsp;number&nbsp;of&nbsp;requests&nbsp;to&nbsp;allow#&nbsp;during&nbsp;a&nbsp;persistent&nbsp;connection.&nbsp;Set&nbsp;to&nbsp;0&nbsp;to&nbsp;allow&nbsp;an&nbsp;unlimited&nbsp;amount.#&nbsp;We&nbsp;recommend&nbsp;you&nbsp;leave&nbsp;this&nbsp;number&nbsp;high,&nbsp;for&nbsp;maximum&nbsp;performance.#MaxKeepAliveRequests&nbsp;100##&nbsp;KeepAliveTimeout:&nbsp;Number&nbsp;of&nbsp;seconds&nbsp;to&nbsp;wait&nbsp;for&nbsp;the&nbsp;next&nbsp;request&nbsp;from&nbsp;the#&nbsp;same&nbsp;client&nbsp;on&nbsp;the&nbsp;same&nbsp;connection.#KeepAliveTimeout&nbsp;5##&nbsp;UseCanonicalName:&nbsp;Determines&nbsp;how&nbsp;Apache&nbsp;constructs&nbsp;self-referencing&nbsp;#&nbsp;URLs&nbsp;and&nbsp;the&nbsp;SERVER_NAME&nbsp;and&nbsp;SERVER_PORT&nbsp;variables.#&nbsp;When&nbsp;set&nbsp;&quot;Off&quot;,&nbsp;Apache&nbsp;will&nbsp;use&nbsp;the&nbsp;Hostname&nbsp;and&nbsp;Port&nbsp;supplied#&nbsp;by&nbsp;the&nbsp;client.&nbsp;When&nbsp;set&nbsp;&quot;On&quot;,&nbsp;Apache&nbsp;will&nbsp;use&nbsp;the&nbsp;value&nbsp;of&nbsp;the#&nbsp;ServerName&nbsp;directive.#UseCanonicalName&nbsp;Off##&nbsp;AccessFileName:&nbsp;The&nbsp;name&nbsp;of&nbsp;the&nbsp;file&nbsp;to&nbsp;look&nbsp;for&nbsp;in&nbsp;each&nbsp;directory#&nbsp;for&nbsp;additional&nbsp;configuration&nbsp;directives.&nbsp;See&nbsp;also&nbsp;the&nbsp;AllowOverride&nbsp;#&nbsp;directive.#AccessFileName&nbsp;.htaccess##&nbsp;ServerTokens#&nbsp;This&nbsp;directive&nbsp;configures&nbsp;what&nbsp;you&nbsp;return&nbsp;as&nbsp;the&nbsp;Server&nbsp;HTTP&nbsp;response#&nbsp;Header.&nbsp;The&nbsp;default&nbsp;is&nbsp;&#39;Full&#39;&nbsp;which&nbsp;sends&nbsp;information&nbsp;about&nbsp;the&nbsp;OS-Type#&nbsp;and&nbsp;compiled&nbsp;in&nbsp;modules.#&nbsp;Set&nbsp;to&nbsp;one&nbsp;of:&nbsp;Full&nbsp;|&nbsp;OS&nbsp;|&nbsp;Minor&nbsp;|&nbsp;Minimal&nbsp;|&nbsp;Major&nbsp;|&nbsp;Prod#&nbsp;where&nbsp;Full&nbsp;conveys&nbsp;the&nbsp;most&nbsp;information,&nbsp;and&nbsp;Prod&nbsp;the&nbsp;least.#ServerTokens&nbsp;Full##&nbsp;Optionally&nbsp;add&nbsp;a&nbsp;line&nbsp;containing&nbsp;the&nbsp;server&nbsp;version&nbsp;and&nbsp;virtual&nbsp;host#&nbsp;name&nbsp;to&nbsp;server-generated&nbsp;pages&nbsp;(internal&nbsp;error&nbsp;documents,&nbsp;FTP&nbsp;directory&nbsp;#&nbsp;listings,&nbsp;mod_status&nbsp;and&nbsp;mod_info&nbsp;output&nbsp;etc.,&nbsp;but&nbsp;not&nbsp;CGI&nbsp;generated&nbsp;#&nbsp;documents&nbsp;or&nbsp;custom&nbsp;error&nbsp;documents).#&nbsp;Set&nbsp;to&nbsp;&quot;EMail&quot;&nbsp;to&nbsp;also&nbsp;include&nbsp;a&nbsp;mailto:&nbsp;link&nbsp;to&nbsp;the&nbsp;ServerAdmin.#&nbsp;Set&nbsp;to&nbsp;one&nbsp;of:&nbsp;On&nbsp;|&nbsp;Off&nbsp;|&nbsp;EMail#ServerSignature&nbsp;On##&nbsp;HostnameLookups:&nbsp;Log&nbsp;the&nbsp;names&nbsp;of&nbsp;clients&nbsp;or&nbsp;just&nbsp;their&nbsp;IP&nbsp;addresses#&nbsp;e.g.,&nbsp;www.apache.org&nbsp;(on)&nbsp;or&nbsp;204.62.129.132&nbsp;(off).#&nbsp;The&nbsp;default&nbsp;is&nbsp;off&nbsp;because&nbsp;it&#39;d&nbsp;be&nbsp;overall&nbsp;better&nbsp;for&nbsp;the&nbsp;net&nbsp;if&nbsp;people#&nbsp;had&nbsp;to&nbsp;knowingly&nbsp;turn&nbsp;this&nbsp;feature&nbsp;on,&nbsp;since&nbsp;enabling&nbsp;it&nbsp;means&nbsp;that#&nbsp;each&nbsp;client&nbsp;request&nbsp;will&nbsp;result&nbsp;in&nbsp;AT&nbsp;LEAST&nbsp;one&nbsp;lookup&nbsp;request&nbsp;to&nbsp;the#&nbsp;nameserver.#HostnameLookups&nbsp;Off</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">看了下,这些是 Apache 的默认配置,Apache 也没有 include 到 httpd.conf 当中。因此,编辑 httpd.conf,找到如下参数:</p><pre class="lang:default decode:true hljs apache" style="box-sizing: border-box; font-size: 1em; font-family: &quot;Lucida Console&quot;, Monaco, &quot;Courier New&quot;; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">#Include&nbsp;conf/extra/httpd-default.conf</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">去掉注释,保存文件。然后再编辑&nbsp;/usr/local/apache2/conf/extra/httpd-default.conf 文件,将 Timeout 的值修改为符合生产环境要求的 1800 秒,最后执行 Apache 平滑重启命令即可:</p><pre class="lang:default decode:true hljs bash" style="box-sizing: border-box; font-size: 1em; font-family: &quot;Lucida Console&quot;, Monaco, &quot;Courier New&quot;; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">/usr/local/apache2/bin/apachectl&nbsp;-k&nbsp;graceful或者/usr/local/apache2/bin/httpd&nbsp;-k&nbsp;graceful</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">Ps:我之前一直以为只有 Nginx 有一个平滑 reload 命令,后面才知道 Apache、Haproxy 都支持平滑重启名称,这个非常棒!</p><p style="box-sizing: border-box; margin-top: 0px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9; margin-bottom: 0px !important;">重载之后,就不会出现 504 网关超时设置了。</p></article><span class="fab fa-weibo" style="box-sizing: border-box; -webkit-font-smoothing: antialiased; display: inline-block; font-variant-numeric: normal; font-variant-east-asian: normal; text-rendering: auto; line-height: 34px; font-family: &quot;Font Awesome 5 Brands&quot;; background: radial-gradient(circle at 75% 25%, rgb(255, 122, 96) 0px, rgb(230, 22, 45) 100%); transition: all 0.5s ease 0s; color: rgb(255, 255, 255); width: 32px; height: 32px; border-radius: 50%; margin-right: 4px; font-size: 16px; text-align: center; box-shadow: rgba(255, 73, 73, 0.6) 0px 10px 10px -2px;"></span><p><br/></p>
本文链接
返回本栏目
工单