• 09 nginx 中 upstream max_conns 配置为 1, 导致的一部分静态资源请求成功, 一部分静态资源请求失败


    前言

    呵呵 这是之前搭建一个项目的时候出现的问题 

    主要的情况是 一个 nginx, 代理了上游的服务, 设置的 max_conns 为 1, 然后 之后 nginx 来请求上游的服务的时候 一部分请求成功, 一部分请求失败  

    这里 我们来从 源码层面上 剖析一下这个问题, 以及 max_conns 的理解 

    测试用例

    nginx 配置文件如下 

    1. # upstream list
    2. upstream api_server {
    3. # server localhost:18080 max_conns=1;
    4. server localhost:63344 max_conns=1;
    5. }
    6. // 省略部分其他配置
    7. location / {
    8. #root html;
    9. #index index.html index.htm;
    10. proxy_set_header Host $host;
    11. proxy_pass http://api_server;
    12. }

    上游的服务的 html, 这里会级联获取依赖的 100 个 js 文件 

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>H5DrawImage.html</title>
    6. </head>
    7. <body>
    8. </body>
    9. <script src="./js/js01.js" type="text/javascript"></script>
    10. <script src="./js/js02.js" type="text/javascript"></script>
    11. <script src="./js/js03.js" type="text/javascript"></script>
    12. <script src="./js/js04.js" type="text/javascript"></script>
    13. <script src="./js/js05.js" type="text/javascript"></script>
    14. <script src="./js/js06.js" type="text/javascript"></script>
    15. <script src="./js/js07.js" type="text/javascript"></script>
    16. <script src="./js/js08.js" type="text/javascript"></script>
    17. <script src="./js/js09.js" type="text/javascript"></script>
    18. <script src="./js/js10.js" type="text/javascript"></script>
    19. <script src="./js/js11.js" type="text/javascript"></script>
    20. <script src="./js/js12.js" type="text/javascript"></script>
    21. <script src="./js/js13.js" type="text/javascript"></script>
    22. <script src="./js/js14.js" type="text/javascript"></script>
    23. <script src="./js/js15.js" type="text/javascript"></script>
    24. <script src="./js/js16.js" type="text/javascript"></script>
    25. <script src="./js/js17.js" type="text/javascript"></script>
    26. <script src="./js/js18.js" type="text/javascript"></script>
    27. <script src="./js/js19.js" type="text/javascript"></script>
    28. <script src="./js/js20.js" type="text/javascript"></script>
    29. <script src="./js/js21.js" type="text/javascript"></script>
    30. <script src="./js/js22.js" type="text/javascript"></script>
    31. <script src="./js/js23.js" type="text/javascript"></script>
    32. <script src="./js/js24.js" type="text/javascript"></script>
    33. <script src="./js/js25.js" type="text/javascript"></script>
    34. <script src="./js/js26.js" type="text/javascript"></script>
    35. <script src="./js/js27.js" type="text/javascript"></script>
    36. <script src="./js/js28.js" type="text/javascript"></script>
    37. <script src="./js/js29.js" type="text/javascript"></script>
    38. <script src="./js/js30.js" type="text/javascript"></script>
    39. <script src="./js/js31.js" type="text/javascript"></script>
    40. <script src="./js/js32.js" type="text/javascript"></script>
    41. <script src="./js/js33.js" type="text/javascript"></script>
    42. <script src="./js/js34.js" type="text/javascript"></script>
    43. <script src="./js/js35.js" type="text/javascript"></script>
    44. <script src="./js/js36.js" type="text/javascript"></script>
    45. <script src="./js/js37.js" type="text/javascript"></script>
    46. <script src="./js/js38.js" type="text/javascript"></script>
    47. <script src="./js/js39.js" type="text/javascript"></script>
    48. <script src="./js/js40.js" type="text/javascript"></script>
    49. <script src="./js/js41.js" type="text/javascript"></script>
    50. <script src="./js/js42.js" type="text/javascript"></script>
    51. <script src="./js/js43.js" type="text/javascript"></script>
    52. <script src="./js/js44.js" type="text/javascript"></script>
    53. <script src="./js/js45.js" type="text/javascript"></script>
    54. <script src="./js/js46.js" type="text/javascript"></script>
    55. <script src="./js/js47.js" type="text/javascript"></script>
    56. <script src="./js/js48.js" type="text/javascript"></script>
    57. <script src="./js/js49.js" type="text/javascript"></script>
    58. <script src="./js/js50.js" type="text/javascript"></script>
    59. <script src="./js/js51.js" type="text/javascript"></script>
    60. <script src="./js/js52.js" type="text/javascript"></script>
    61. <script src="./js/js53.js" type="text/javascript"></script>
    62. <script src="./js/js54.js" type="text/javascript"></script>
    63. <script src="./js/js55.js" type="text/javascript"></script>
    64. <script src="./js/js56.js" type="text/javascript"></script>
    65. <script src="./js/js57.js" type="text/javascript"></script>
    66. <script src="./js/js58.js" type="text/javascript"></script>
    67. <script src="./js/js59.js" type="text/javascript"></script>
    68. <script src="./js/js60.js" type="text/javascript"></script>
    69. <script src="./js/js61.js" type="text/javascript"></script>
    70. <script src="./js/js62.js" type="text/javascript"></script>
    71. <script src="./js/js63.js" type="text/javascript"></script>
    72. <script src="./js/js64.js" type="text/javascript"></script>
    73. <script src="./js/js65.js" type="text/javascript"></script>
    74. <script src="./js/js66.js" type="text/javascript"></script>
    75. <script src="./js/js67.js" type="text/javascript"></script>
    76. <script src="./js/js68.js" type="text/javascript"></script>
    77. <script src="./js/js69.js" type="text/javascript"></script>
    78. <script src="./js/js70.js" type="text/javascript"></script>
    79. <script src="./js/js71.js" type="text/javascript"></script>
    80. <script src="./js/js72.js" type="text/javascript"></script>
    81. <script src="./js/js73.js" type="text/javascript"></script>
    82. <script src="./js/js74.js" type="text/javascript"></script>
    83. <script src="./js/js75.js" type="text/javascript"></script>
    84. <script src="./js/js76.js" type="text/javascript"></script>
    85. <script src="./js/js77.js" type="text/javascript"></script>
    86. <script src="./js/js78.js" type="text/javascript"></script>
    87. <script src="./js/js79.js" type="text/javascript"></script>
    88. <script src="./js/js80.js" type="text/javascript"></script>
    89. <script src="./js/js81.js" type="text/javascript"></script>
    90. <script src="./js/js82.js" type="text/javascript"></script>
    91. <script src="./js/js83.js" type="text/javascript"></script>
    92. <script src="./js/js84.js" type="text/javascript"></script>
    93. <script src="./js/js85.js" type="text/javascript"></script>
    94. <script src="./js/js86.js" type="text/javascript"></script>
    95. <script src="./js/js87.js" type="text/javascript"></script>
    96. <script src="./js/js88.js" type="text/javascript"></script>
    97. <script src="./js/js89.js" type="text/javascript"></script>
    98. <script src="./js/js90.js" type="text/javascript"></script>
    99. <script src="./js/js91.js" type="text/javascript"></script>
    100. <script src="./js/js92.js" type="text/javascript"></script>
    101. <script src="./js/js93.js" type="text/javascript"></script>
    102. <script src="./js/js94.js" type="text/javascript"></script>
    103. <script src="./js/js95.js" type="text/javascript"></script>
    104. <script src="./js/js96.js" type="text/javascript"></script>
    105. <script src="./js/js97.js" type="text/javascript"></script>
    106. <script src="./js/js98.js" type="text/javascript"></script>
    107. <script src="./js/js99.js" type="text/javascript"></script>
    108. <script>
    109. </script>
    110. </html>

    从浏览器访问该服务如下, 确保 服务正常  

    nginx 代理访问该服务状态如下, 可以看到 绝大多数的请求 502 请求失败了 

    但是 依然存在一部分请求, 正常响应 

    参数 max_conns    

    以下截图来自于官方文档 Module ngx_http_upstream_module

    可以看到 其实就是对于 upstream 的一个保护, 做了一个 并发量 控制  

    但是 我们这里只有一个 worker ! 

    但是我们考虑另外一个问题, 在之前的调试过程中我们能够发现的是 worker process 是只有一个线程, 那么 即使是客户端这边发送了一百多个请求过来, 那么这个 worker process 也应该是一个请求一个请求的处理呀? 怎么会出现这种情况 ??

    nginx 的请求处理方式

    nginx 的请求处理调试可以参考文章 nginx 的一次请求处理调试 

    是根据事件来处理 

    也就是 接受请求, 和向上游发送代理请求, 接受上游的代理响应 这三者 不是串行的 

    那么可能是 接受了请求1, 接受了请求2, 处理请求1 处理成功, 接着处理请求2, 因为请求1还没有处理完成, 因此处理请求2 的时候会造成超过 max_conns 的限制, 导致处理失败 

    正常情况应该是走如下 peers->single 下面的 max_conns 的校验, 然后返回 NGX_BUSY 

    但是 mac 这里似乎我 api_servers 只配置了一个 后台服务, 这里 peers 中却有两个节点?? 呵呵 这个多久看一下 

    然后 在上层 NGX_BUSY, 响应给了客户端 502 

    我们在接受请求的地方, 发送代理请求的地方, 接受上游的代理响应的地方 加上日志输出 

    可以看到的是 如下几个特征

    1. recv 之后的 下一个请求应该就是下一次发送成功的请求

    2. recv 之后的下一个 accept 到这个 accept 的 recv 之间所有接受的请求都是 502 处理失败 

    1. accept http - /HelloWorld/getMultiResource/GetMultiResource.html HTTP/1.1
    2. send - /HelloWorld/getMultiResource/GetMultiResource.html HTTP/1.1
    3. recv - /HelloWorld/getMultiResource/GetMultiResource.html HTTP/1.1
    4. accept http - /HelloWorld/getMultiResource/js/js01.js HTTP/1.1
    5. accept http - /HelloWorld/getMultiResource/js/js02.js HTTP/1.1
    6. accept http - /50x.html
    7. send - /HelloWorld/getMultiResource/js/js01.js HTTP/1.1
    8. accept http - /HelloWorld/getMultiResource/js/js03.js HTTP/1.1
    9. accept http - /50x.html
    10. accept http - /HelloWorld/getMultiResource/js/js04.js HTTP/1.1
    11. accept http - /50x.html
    12. accept http - /HelloWorld/getMultiResource/js/js05.js HTTP/1.1
    13. accept http - /50x.html
    14. accept http - /HelloWorld/getMultiResource/js/js06.js HTTP/1.1
    15. accept http - /50x.html
    16. accept http - /HelloWorld/getMultiResource/js/js07.js HTTP/1.1
    17. accept http - /50x.html
    18. recv - /HelloWorld/getMultiResource/js/js01.js HTTP/1.1
    19. accept http - /HelloWorld/getMultiResource/js/js08.js HTTP/1.1
    20. accept http - /HelloWorld/getMultiResource/js/js09.js HTTP/1.1
    21. accept http - /50x.html
    22. accept http - /HelloWorld/getMultiResource/js/js10.js HTTP/1.1
    23. accept http - /50x.html
    24. accept http - /HelloWorld/getMultiResource/js/js11.js HTTP/1.1
    25. accept http - /50x.html
    26. accept http - /HelloWorld/getMultiResource/js/js12.js HTTP/1.1
    27. accept http - /50x.html
    28. accept http - /HelloWorld/getMultiResource/js/js13.js HTTP/1.1
    29. accept http - /50x.html
    30. accept http - /HelloWorld/getMultiResource/js/js14.js HTTP/1.1
    31. accept http - /50x.html
    32. send - /HelloWorld/getMultiResource/js/js08.js HTTP/1.1
    33. accept http - /HelloWorld/getMultiResource/js/js15.js HTTP/1.1
    34. accept http - /50x.html
    35. accept http - /HelloWorld/getMultiResource/js/js16.js HTTP/1.1
    36. accept http - /50x.html
    37. accept http - /HelloWorld/getMultiResource/js/js17.js HTTP/1.1
    38. accept http - /50x.html
    39. accept http - /HelloWorld/getMultiResource/js/js18.js HTTP/1.1
    40. accept http - /50x.html
    41. accept http - /HelloWorld/getMultiResource/js/js19.js HTTP/1.1
    42. accept http - /50x.html
    43. recv - /HelloWorld/getMultiResource/js/js08.js HTTP/1.1
    44. accept http - /HelloWorld/getMultiResource/js/js20.js HTTP/1.1
    45. accept http - /HelloWorld/getMultiResource/js/js21.js HTTP/1.1
    46. accept http - /50x.html
    47. accept http - /HelloWorld/getMultiResource/js/js22.js HTTP/1.1
    48. accept http - /50x.html
    49. accept http - /HelloWorld/getMultiResource/js/js23.js HTTP/1.1
    50. accept http - /50x.html
    51. accept http - /HelloWorld/getMultiResource/js/js24.js HTTP/1.1
    52. accept http - /50x.html
    53. accept http - /HelloWorld/getMultiResource/js/js25.js HTTP/1.1
    54. accept http - /50x.html
    55. accept http - /HelloWorld/getMultiResource/js/js26.js HTTP/1.1
    56. accept http - /50x.html
    57. accept http - /HelloWorld/getMultiResource/js/js27.js HTTP/1.1
    58. accept http - /50x.html
    59. send - /HelloWorld/getMultiResource/js/js20.js HTTP/1.1
    60. accept http - /HelloWorld/getMultiResource/js/js28.js HTTP/1.1
    61. accept http - /HelloWorld/getMultiResource/js/js29.js HTTP/1.1
    62. accept http - /50x.html
    63. accept http - /HelloWorld/getMultiResource/js/js30.js HTTP/1.1
    64. accept http - /50x.html
    65. accept http - /HelloWorld/getMultiResource/js/js31.js HTTP/1.1
    66. accept http - /50x.html
    67. accept http - /HelloWorld/getMultiResource/js/js32.js HTTP/1.1
    68. accept http - /50x.html
    69. recv - /HelloWorld/getMultiResource/js/js20.js HTTP/1.1
    70. accept http - /HelloWorld/getMultiResource/js/js33.js HTTP/1.1
    71. accept http - /HelloWorld/getMultiResource/js/js34.js HTTP/1.1
    72. accept http - /50x.html
    73. accept http - /HelloWorld/getMultiResource/js/js35.js HTTP/1.1
    74. accept http - /50x.html
    75. accept http - /50x.html
    76. accept http - /HelloWorld/getMultiResource/js/js36.js HTTP/1.1
    77. accept http - /50x.html
    78. accept http - /HelloWorld/getMultiResource/js/js37.js HTTP/1.1
    79. accept http - /50x.html
    80. accept http - /HelloWorld/getMultiResource/js/js38.js HTTP/1.1
    81. accept http - /50x.html
    82. accept http - /HelloWorld/getMultiResource/js/js39.js HTTP/1.1
    83. accept http - /50x.html
    84. send - /HelloWorld/getMultiResource/js/js33.js HTTP/1.1
    85. accept http - /HelloWorld/getMultiResource/js/js40.js HTTP/1.1
    86. accept http - /50x.html
    87. accept http - /HelloWorld/getMultiResource/js/js41.js HTTP/1.1
    88. accept http - /50x.html
    89. accept http - /HelloWorld/getMultiResource/js/js42.js HTTP/1.1
    90. accept http - /50x.html
    91. accept http - /HelloWorld/getMultiResource/js/js43.js HTTP/1.1
    92. accept http - /50x.html
    93. accept http - /HelloWorld/getMultiResource/js/js44.js HTTP/1.1
    94. accept http - /50x.html
    95. recv - /HelloWorld/getMultiResource/js/js33.js HTTP/1.1
    96. accept http - /HelloWorld/getMultiResource/js/js45.js HTTP/1.1
    97. accept http - /HelloWorld/getMultiResource/js/js46.js HTTP/1.1
    98. accept http - /50x.html
    99. accept http - /HelloWorld/getMultiResource/js/js47.js HTTP/1.1
    100. accept http - /50x.html
    101. accept http - /HelloWorld/getMultiResource/js/js48.js HTTP/1.1
    102. accept http - /50x.html
    103. accept http - /HelloWorld/getMultiResource/js/js49.js HTTP/1.1
    104. accept http - /50x.html
    105. accept http - /HelloWorld/getMultiResource/js/js50.js HTTP/1.1
    106. accept http - /50x.html
    107. accept http - /HelloWorld/getMultiResource/js/js51.js HTTP/1.1
    108. accept http - /50x.html
    109. accept http - /HelloWorld/getMultiResource/js/js52.js HTTP/1.1
    110. accept http - /50x.html
    111. send - /HelloWorld/getMultiResource/js/js45.js HTTP/1.1
    112. accept http - /HelloWorld/getMultiResource/js/js53.js HTTP/1.1
    113. accept http - /50x.html
    114. accept http - /HelloWorld/getMultiResource/js/js54.js HTTP/1.1
    115. accept http - /50x.html
    116. accept http - /HelloWorld/getMultiResource/js/js55.js HTTP/1.1
    117. accept http - /50x.html
    118. accept http - /HelloWorld/getMultiResource/js/js56.js HTTP/1.1
    119. accept http - /50x.html
    120. accept http - /HelloWorld/getMultiResource/js/js57.js HTTP/1.1
    121. accept http - /50x.html
    122. recv - /HelloWorld/getMultiResource/js/js45.js HTTP/1.1
    123. accept http - /HelloWorld/getMultiResource/js/js58.js HTTP/1.1
    124. accept http - /HelloWorld/getMultiResource/js/js59.js HTTP/1.1
    125. accept http - /50x.html
    126. accept http - /HelloWorld/getMultiResource/js/js60.js HTTP/1.1
    127. accept http - /50x.html
    128. accept http - /HelloWorld/getMultiResource/js/js61.js HTTP/1.1
    129. accept http - /50x.html
    130. accept http - /HelloWorld/getMultiResource/js/js62.js HTTP/1.1
    131. accept http - /50x.html
    132. accept http - /HelloWorld/getMultiResource/js/js63.js HTTP/1.1
    133. accept http - /50x.html
    134. accept http - /HelloWorld/getMultiResource/js/js64.js HTTP/1.1
    135. accept http - /50x.html
    136. accept http - /HelloWorld/getMultiResource/js/js65.js HTTP/1.1
    137. accept http - /50x.html
    138. send - /HelloWorld/getMultiResource/js/js58.js HTTP/1.1
    139. accept http - /HelloWorld/getMultiResource/js/js66.js HTTP/1.1
    140. accept http - /50x.html
    141. accept http - /HelloWorld/getMultiResource/js/js67.js HTTP/1.1
    142. accept http - /50x.html
    143. accept http - /HelloWorld/getMultiResource/js/js68.js HTTP/1.1
    144. accept http - /50x.html
    145. accept http - /HelloWorld/getMultiResource/js/js69.js HTTP/1.1
    146. accept http - /50x.html
    147. accept http - /HelloWorld/getMultiResource/js/js70.js HTTP/1.1
    148. accept http - /50x.html
    149. recv - /HelloWorld/getMultiResource/js/js58.js HTTP/1.1
    150. accept http - /HelloWorld/getMultiResource/js/js71.js HTTP/1.1
    151. accept http - /HelloWorld/getMultiResource/js/js72.js HTTP/1.1
    152. accept http - /50x.html
    153. accept http - /HelloWorld/getMultiResource/js/js73.js HTTP/1.1
    154. accept http - /50x.html
    155. accept http - /HelloWorld/getMultiResource/js/js74.js HTTP/1.1
    156. accept http - /50x.html
    157. accept http - /HelloWorld/getMultiResource/js/js75.js HTTP/1.1
    158. accept http - /50x.html
    159. accept http - /HelloWorld/getMultiResource/js/js76.js HTTP/1.1
    160. accept http - /50x.html
    161. accept http - /HelloWorld/getMultiResource/js/js77.js HTTP/1.1
    162. accept http - /50x.html
    163. accept http - /HelloWorld/getMultiResource/js/js78.js HTTP/1.1
    164. accept http - /50x.html
    165. send - /HelloWorld/getMultiResource/js/js71.js HTTP/1.1
    166. accept http - /HelloWorld/getMultiResource/js/js79.js HTTP/1.1
    167. accept http - /50x.html
    168. accept http - /HelloWorld/getMultiResource/js/js80.js HTTP/1.1
    169. accept http - /50x.html
    170. accept http - /HelloWorld/getMultiResource/js/js81.js HTTP/1.1
    171. accept http - /50x.html
    172. accept http - /HelloWorld/getMultiResource/js/js82.js HTTP/1.1
    173. accept http - /50x.html
    174. accept http - /HelloWorld/getMultiResource/js/js83.js HTTP/1.1
    175. accept http - /50x.html
    176. recv - /HelloWorld/getMultiResource/js/js71.js HTTP/1.1
    177. accept http - /HelloWorld/getMultiResource/js/js84.js HTTP/1.1
    178. accept http - /HelloWorld/getMultiResource/js/js85.js HTTP/1.1
    179. accept http - /50x.html
    180. accept http - /HelloWorld/getMultiResource/js/js86.js HTTP/1.1
    181. accept http - /50x.html
    182. accept http - /HelloWorld/getMultiResource/js/js87.js HTTP/1.1
    183. accept http - /50x.html
    184. accept http - /HelloWorld/getMultiResource/js/js88.js HTTP/1.1
    185. accept http - /50x.html
    186. accept http - /HelloWorld/getMultiResource/js/js89.js HTTP/1.1
    187. accept http - /50x.html
    188. accept http - /HelloWorld/getMultiResource/js/js90.js HTTP/1.1
    189. accept http - /50x.html
    190. accept http - /HelloWorld/getMultiResource/js/js91.js HTTP/1.1
    191. accept http - /50x.html
    192. send - /HelloWorld/getMultiResource/js/js84.js HTTP/1.1
    193. accept http - /HelloWorld/getMultiResource/js/js92.js HTTP/1.1
    194. accept http - /50x.html
    195. accept http - /HelloWorld/getMultiResource/js/js93.js HTTP/1.1
    196. accept http - /50x.html
    197. accept http - /HelloWorld/getMultiResource/js/js94.js HTTP/1.1
    198. accept http - /50x.html
    199. accept http - /HelloWorld/getMultiResource/js/js95.js HTTP/1.1
    200. accept http - /50x.html
    201. accept http - /HelloWorld/getMultiResource/js/js96.js HTTP/1.1
    202. accept http - /50x.html
    203. recv - /HelloWorld/getMultiResource/js/js84.js HTTP/1.1
    204. accept http - /HelloWorld/getMultiResource/js/js97.js HTTP/1.1
    205. accept http - /HelloWorld/getMultiResource/js/js98.js HTTP/1.1
    206. accept http - /50x.html
    207. accept http - /HelloWorld/getMultiResource/js/js99.js HTTP/1.1
    208. accept http - /50x.html
    209. send - /HelloWorld/getMultiResource/js/js97.js HTTP/1.1
    210. recv - /HelloWorld/getMultiResource/js/js97.js HTTP/1.1
    211. accept http - /favicon.ico HTTP/1.1
    212. send - /favicon.ico HTTP/1.1
    213. recv - /favicon.ico HTTP/1.1

    我们来看一下 这里请求成功的这一部分请求

  • 相关阅读:
    Java学习笔记3.8.1 内部类 - 成员内部类
    基于.NetCore开发博客项目 StarBlog - (7) 页面开发之文章详情页面
    数据结构学习笔记——多维数组、矩阵与广义表
    Lesson4-1:OpenCV图像特征提取与描述---角点特征
    heapq制作优先级队列
    ES6中对Promise的理解以及它的实例方法
    Spring Cloud Alibaba微服务第11章之MyBatis-plus
    Spring框架(缺SM整合)
    Flink1.15源码解析--启动TaskManager
    CyberController手机外挂番外篇:源代码的二次修改
  • 原文地址:https://blog.csdn.net/u011039332/article/details/125835131