美文网首页
nginx解析大头部参数的分析.md

nginx解析大头部参数的分析.md

作者: ShootHzj | 来源:发表于2019-08-26 04:41 被阅读0次

    参考

    http://nginx.org/en/docs/http/ngx_http_core_module.html#client_header_buffer_size
    http://nginx.org/en/docs/http/ngx_http_core_module.html
    https://github.com/nginx/nginx
    

    需求背景

    在对接一个三方系统的时候,需要处理头部消息超过8k的消息,因为nginx默认的client_header_buffer_size是1k,需要调整对应的参数

    分析过程

    • 查阅资料,发现两个参数影响大头部的处理,分别是client_header_buffer_size和large_client_header_buffers
      client_header_buffer_size
      Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated.
      设置读取客户端http请求的buffer大小.对于大多数请求而言,1k已经足够了.不过,如果一个请求包含了很长的cookie,或者来自于WAP客户端(具体我也不太了解,上网搜索了一下,大概原因应该是WAP客户端对HTTP头部做了增强,加入了很多自己的头部).如果大小超过了client_header_buffer_size就要从large_client_header_buffers分配
      
      large_client_header_buffers
      Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released.
      

    设置用于读取大头部buffer的最大数量和大小.如果request line超过这个buffer的最大大小,那么会返回414.如果是头部,那么会返回400.缓冲区仅按需分配.默认情况下,缓冲区大小等于8K字节.如果在请求处理结束后连接转换为保持活动状态,那么这些buffer将被释放

    • 测试过程中发现,client_header_buffer_size和large_client_header_buffers,仅能在main块和server块生效,不能在location块生效,查看资料,根据nginx处理HTTP请求的11个阶段,查找匹配location在读取头部之后,所以不能在location块生效.同时client_header_buffer_size的注册也在create_srv_conf流程中
      typedef enum {
        // 在接收到完整的HTTP头部后处理的HTTP阶段,我们就在这里
        NGX_HTTP_POST_READ_PHASE = 0,
        NGX_HTTP_SERVER_REWRITE_PAHSE,
        //查找location块,并匹配
        NGX_HTTP_FIND_CONFIG_PHASE,
        NGX_HTTP_REWRITE_PHASE,
        NGX_HTTP_POST_REWRITE_PHASE,
        NGX_HTTP_ACCESS_PHASE,
        NGX_HTTP_POST_ACCESS_PHASE,
        NGX_HTTP_TRY_FILES_PHASE,
        NGX_HTTP_CONTENT_PHASE,
        NGX_HTTP_LOG_PHASE,
      } ngx_http_phases;
      

    相关文章

      网友评论

          本文标题:nginx解析大头部参数的分析.md

          本文链接:https://www.haomeiwen.com/subject/tiylectx.html