When I’m learning nginx, because it has a lot of data structures, I usually forget it after reading it. So record it here while you study, so you can check it later.
ngx_buf_t: Buffer node
<code><span>1</span>: <span>typedef</span><span>struct</span> ngx_buf_s ngx_buf_t; <span>2</span>: <span>typedef</span><span>void</span> *ngx_buf_tag_t; <span>3</span>: <span>struct</span> ngx_buf_s { <span>4</span>: <span>/* 5: * pos通常是用来告诉使用者本次应该从pos这个位置开始处理内存中的数据,这样设置是因为同一个 6: * ngx_buf_t可能被多次反复处理。当然,pos的含义是由使用它的模板定义的 7: */</span><span>8</span>: u_char *pos; <span>9</span>: <span>10</span>: <span>/* last通常表示有效的内容到此为止,注意,pos与last之间的内存是希望nginx处理的内容 */</span><span>11</span>: u_char *last; <span>12</span>: <span>13</span>: <span>/* 14: * 处理文件时,file_pos与file_last的含义与处理内存时的pos与last相同, 15: * file_pos表示将要处理的文件位置,file_last表示截至的文件位置。 16: */</span><span>17</span>: off_t file_pos; <span>18</span>: off_t file_last; <span>19</span>: <span>20</span>: <span>/* 如果ngx_buf_t缓冲区用于内存,那么start指向这段内存的起始地址 */</span><span>21</span>: u_char *start; <span>22</span>: <span>23</span>: <span>/* 与start成员对应,指向缓冲区内存的末尾 */</span><span>24</span>: u_char *end; <span>25</span>: <span>26</span>: <span>/* 表示当前缓冲区的类型,例如由哪个模块使用就指向这个模块ngx_module_t变量的地址 */</span><span>27</span>: ngx_buf_tag_t tag; <span>28</span>: <span>29</span>: <span>/* 引用的文件 */</span><span>30</span>: ngx_file_t *file; <span>31</span>: <span>32</span>: <span>/* 33: * 当前缓冲区的影子缓冲区,该成员很少用到。当缓冲区转发上游服务器的响应时才使用了shadow成员, 34: * 这是因为nginx太节约内存了,分配一块内存并使用ngx_buf_t表示接收到的上游服务器响应后, 35: * 在向下游客户端转发时可能会把这块内存存储到文件中,也可能直接向下游发送,此时nginx绝对不会 36: * 重新复制一份内存用于新的目的,而是再次建立一个ngx_buf_t结构体指向原内存,这样多个ngx_buf_t 37: * 结构体指向了同一份内存,它们之间的关系就通过shadow成员来引用,一般不建议使用。 38: */</span><span>39</span>: ngx_buf_t *shadow; <span>40</span>: <span>41</span>: <span>/* 临时内存标志位,为1时表示数据在内存中且这段内存可以修改 */</span><span>42</span>: <span>unsigned</span> temporay:<span>1</span>; <span>43</span>: <span>44</span>: <span>/* 标志位,为1时表示数据在内存中且这段内存不可以修改 */</span><span>45</span>: <span>unsigned</span> memory:<span>1</span>; <span>46</span>: <span>47</span>: <span>/* 标志位,为1时表示这段内存是用nmap系统调用映射过来的,不可以修改 */</span><span>48</span>: <span>unsigned</span> mmap:<span>1</span>; <span>49</span>: <span>50</span>: <span>/* 标志位,为1时表示可回收 */</span><span>51</span>: <span>unsigned</span> recycled:<span>1</span>; <span>52</span>: <span>53</span>: <span>/* 标志位,为1时表示这段缓冲区处理的是文件而不是内存 */</span><span>54</span>: <span>unsigned</span> in_file:<span>1</span>; <span>55</span>: <span>56</span>: <span>/* 标志位,为1时表示需要执行flush操作 */</span><span>57</span>: <span>unsigned</span> flush:<span>1</span>; <span>58</span>: <span>59</span>: <span>/* 60: * 标志位,对于操作这块缓冲区时是否使用同步方式,需谨慎考虑,这可能会阻塞nginx进程,nginx中所有 61: * 操作几乎都是异步的,这是它支持高并发的关键。有些框架代码在sync为1时可能会有阻塞的方式进行I/O 62: * 操作,它的意义视使用它的nginx模块而定。 63: */</span><span>64</span>: <span>unsigned</span> sync:<span>1</span>; <span>65</span>: <span>66</span>: <span>/* 67: * 标志位,表示是否是最后一块缓冲区,因为ngx_buf_t可以由ngx_chain_t链表串联起来,因此为1时, 68: * 表示当前是最后一块待处理的缓冲区。 69: */</span><span>70</span>: <span>unsigned</span> last_buf:<span>1</span>; <span>71</span>: <span>72</span>: <span>/* 标志位,表示是否是ngx_chain_t中的最后一块缓冲区 */</span><span>73</span>: <span>unsigned</span> last_in_chain:<span>1</span>; <span>74</span>: <span>75</span>: <span>/* 标志位,表示是否是最后一个影子缓冲区,与shadow域配合使用。通常不建议使用它 */</span><span>76</span>: <span>unsigned</span> last_shadow:<span>1</span>; <span>77</span>: <span>78</span>: <span>/* 标志位,表示当前缓冲区是否属于临时文件 */</span><span>79</span>: <span>unsigned</span> temp_file:<span>1</span>; <span>80</span>: }</code>
ngx_chain_t: Buffer chain list
<code><span>typedef</span><span>struct</span> ngx_chain_s ngx_chain_t; <span>struct</span> ngx_chain_s { ngx_buf_t *buf; <span>//buf指向当前的ngx_buf_t缓冲区</span> ngx_chain_t *next; <span>//next则用来指向下一个ngx_chain_t,如果这是最后一个ngx_chain_t,则需要把next置为NULL。</span> };</code>
ngx_http_upstream_t: Set the restrictive parameters of upstream
<code>typedef struct{ <span>...</span> //连接上游服务器的超时时间,单位为毫秒 ngx_msec_t connect_timeout; //发送TCP包到上游服务器的超时时间,单位为毫秒 ngx_msec_t connect_timeout; //接收TCP包到上游服务器的超时时间,单位为毫秒 ngx_msec_t connect_timeout; <span>...</span> }ngx_http_upstream_conf_t;</code>
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
The above introduces the data structure collection of nginx (updated at any time), including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.