WEB应用

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

Jager · 8月13日 · 2016年 4650次已读

某日,组内后台开发找到我,问我们的 WEB 服务器超时设置是多少。他反馈的问题是,有一个 VLAN 切换任务 cgi 接口经常返回 504 网关超时错误,要我分析解决下。修改Apache的超时设置,解决长连接请求超时问题

我问了一下,得知这个请求遇到网络设备对象较多的时候,需要小半个小时才能完成,也就是要用到长连接才行。

老规矩,从开发那拿到接口地址,得到接入层服务器 IP,是一台 Haproxy 代理,看了一下 Haproxy 的超时设置:

# 设置成功连接到一台服务器的最长等待时间,默认单位是毫秒,新版本的 haproxy 使用 timeout connect 替代,该参数向后兼容
contimeout 3600
# 设置连接客户端发送数据时的成功连接最长等待时间,默认单位是毫秒,新版本 haproxy 使用 timeout client 替代。该参数向后兼容
clitimeout 3600
# 设置服务器端回应客户度数据发送的最长等待时间,默认单位是毫秒,新版本 haproxy 使用 timeout server 替代。该参数向后兼容
srvtimeout 3600

各种 1 小时超时,所以排除 Haproxy 的影响,继续往下看。

Haproxy 代理的是 2 台 Apache,也就是部署了 cgi 接口的服务器。第一时间查看了 httpd.conf 和 httpd-vhost.conf 中的配置,居然没找到超时设置。

于是,搜索了下相关教程,发现原来藏在了 httpd-default.conf 当中:

#
# This configuration file reflects default settings for Apache HTTP Server.
#
# You may change these, but chances are that you may not need to.
#

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5

#
# UseCanonicalName: Determines how Apache constructs self-referencing 
# URLs and the SERVER_NAME and SERVER_PORT variables.
# When set "Off", Apache will use the Hostname and Port supplied
# by the client. When set "On", Apache will use the value of the
# ServerName directive.
#
UseCanonicalName Off

#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride 
# directive.
#
AccessFileName .htaccess

#
# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of: Full | OS | Minor | Minimal | Major | Prod
# where Full conveys the most information, and Prod the least.
#
ServerTokens Full

#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory 
# listings, mod_status and mod_info output etc., but not CGI generated 
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of: On | Off | EMail
#
ServerSignature On

#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

看了下,这些是 Apache 的默认配置,Apache 也没有 include 到 httpd.conf 当中。因此,编辑 httpd.conf,找到如下参数:

#Include conf/extra/httpd-default.conf

去掉注释,保存文件。然后再编辑 /usr/local/apache2/conf/extra/httpd-default.conf 文件,将 Timeout 的值修改为符合生产环境要求的 1800 秒,最后执行 Apache 平滑重启命令即可:

/usr/local/apache2/bin/apachectl -k graceful
或者
/usr/local/apache2/bin/httpd -k graceful

Ps:我之前一直以为只有 Nginx 有一个平滑 reload 命令,后面才知道 Apache、Haproxy 都支持平滑重启名称,这个非常棒!

重载之后,就不会出现 504 网关超时设置了。

47 条回应
  1. 工业铝型材 2016-8-13 · 16:24

    ...........................不错

  2. 香港云服务器 2016-8-13 · 17:10

    解决长连接请求超时问题,这个问题,搞服务器经常都会遇到,感谢博主分享的方法

  3. Koolight 2016-8-13 · 22:51

    手机版中“分享”跑下面了 :mrgreen:

    • Koolight 2016-8-13 · 22:52

      等待一会就恢复了,不过动态背景后感觉有些卡卡的。

      • 小C博客 2016-8-14 · 19:49

        这个动态背景在手机端是卡的不要不要的!

  4. 我爱动感单车网 2016-8-15 · 13:38

    :smile: 小白一枚,也来学习下!

  5. 松松软文发布 2016-8-15 · 15:10

    现在真是各种问题都有呀。

  6. 购物街 2016-8-16 · 9:44

    谢谢分享。

  7. 成航先森 2016-8-16 · 11:40

    这个教程倒是简单,能看懂了。平滑重启还没接触过。

  8. 易淘金股票博客 2016-8-16 · 12:38

    不错很实用

  9. 龙笑天 2016-8-16 · 17:33

    [color=red]马克一下 以备后用~[/color]

  10. 酷居 2016-8-16 · 19:27

    求老大写一篇:WordPress生成网站访问日志和蜘蛛爬行分析日志的文章,网上虽然也有,但好像都失效了。。。

  11. 易淘金股票博客 2016-8-18 · 8:46

    超时时间设置多少合适呢?

  12. 上海项目管理软件 2016-8-19 · 15:20

    楼主分享好东西,学习了感谢楼主分析

  13. 贝蒂斯官网 2016-8-19 · 16:23

    顶了,博客还是不错的 会常关注学习的~

  14. 跨境电商平台 2016-8-21 · 9:55

    非常不错!!!!

  15. 易淘金股票博客 2016-8-22 · 14:07

    有看到新作 不错不错

  16. 微信投票第三方平台 2016-8-22 · 17:19

    常来博主这里 又更新了 挺好的

  17. PHP程序员雷雪松 2016-8-23 · 15:12

    不错,很有用!!!

  18. 购物街 2016-8-23 · 15:46

    谢谢分享

  19. 西米 2016-8-23 · 16:27

    没看懂

  20. 我爱脚丫网 2016-8-24 · 12:34

    服务器方面的问题还是张哥最专业啊,以后常来。

  21. 易淘金股票博客 2016-8-25 · 10:22

    服务器有时很难搞

  22. 洋得意自媒体 2016-8-25 · 21:28

    感谢分享 欢迎回访

  23. 求学与求知 2016-8-25 · 22:17

    张老师,能否下一篇文章的主题是关于“启用CDN后,如何获取真实的UA”
    感觉和之前的一篇 用CDN后 获取用户真实IP 一个意思。

    想用CDN,但是机器人采集太狠了,文章被转载严重。不用CDN 可以靠UA屏蔽,全部返回403。

    • avatar
      Jager 2016-8-26 · 9:09

      UA是客户端传递过去的,CDN不会改变UA

      • 求学与求知 2016-8-26 · 10:40

        0.0 那这么来看我启用CDN,也并不会妨碍屏蔽的某些采集UA喽。。。
        一直觉得启用了CDN,封UA和封IP一样的无效了就,毕竟它们都在CDN节点上打开内容采集了。

        学习到了。

  24. 松松软文发布 2016-8-26 · 13:41

    有些东西现在用不上不代表以后也用不上,可以收藏一下

  25. 无忧移民人网 2016-8-26 · 16:05

    解决了我得大问题。谢谢博主!

  26. 深圳装修公司 2016-8-26 · 17:18

    感谢博主分享的方法

  27. 小小温哒铺子 2016-8-27 · 15:26

    路过此地,留下个脚印 感谢作者的分享!

  28. 伪极客 2016-8-27 · 19:09

    学习 感谢分享

  29. 求学与求知 2016-8-28 · 16:56

    张老师,时候来一波关于‘MIP引入’的文章了。最近看百度站长新出了这个栏目,和之前【自动推送】差不多的意思,请出招吧,学生们都在等着呢,嘿嘿! 抽空来一下呢~

  30. 外汇交易平台 2016-8-31 · 13:41

    谢谢大神的分享了

  31. 管家婆 2016-9-1 · 11:28

    学习快乐 学习了

  32. 连衣裙 2016-9-1 · 15:16

    满屏代码!!小白只能膜拜!!!支持下!

  33. 网赚藏经阁 2016-9-2 · 22:50

    好多代码啊,看不懂

  34. benen005 2016-9-6 · 8:04

    确实不错

  35. 唯历史 2016-9-6 · 10:22

    看不懂,仅支持

  36. 网盘资源社区 2016-9-18 · 14:58

    这个可以操作,受教 了

  37. 北京监控安装 2016-9-20 · 9:02

    学习了,谢谢!

  38. 云购程序 2016-9-26 · 10:41

    服务器有时间会死不知道怎么回事,请求多了浏览器就没反应了

  39. 高平台球阀 2016-10-11 · 13:07

    很好,学习了。

  40. 养生知识 2017-3-18 · 9:54

    .top的域名是不是不容易收录呀,虽然收录了知宝堂的首页,但是其他页面没有收录,苦恼了,张老师指点一下嘛。