Http request header -- Connection

本文详细解析了HTTP协议中Connection头字段的作用及语法,包括如何指定特定连接选项、HTTP/1.1代理处理Connection头的方式、'close'选项的使用场景等,并强调了Connection头字段不应包含端到端头字段。

Connection

The Connection general-header field allows the sender to specify options that are desired for that particular connection and MUST NOT be communicated by proxies over further connections.

The Connection header has the following grammar:

       Connection = "Connection" ":" 1#(connection-token)
       connection-token  = token

HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token in this field, remove any header field(s) from the message with the same name as the connection-token. Connection options are signaled by the presence of a connection-token in the Connection header field, not by any corresponding additional header field(s), since the additional header field may not be sent if there are no parameters associated with that connection option.

Message headers listed in the Connection header MUST NOT include end-to-end headers, such as Cache-Control.

HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example,

       Connection: close

in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' (section 8.1) after the current request/response is complete.

HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.

A system receiving an HTTP/1.0 (or lower-version) message that includes a Connection header MUST, for each connection-token in this field, remove and ignore any header field(s) from the message with the same name as the connection-token. This protects against mistaken forwarding of such header fields by pre-HTTP/1.1 proxies.


在配置 Spring Boot 应用的 `server` 相关参数时,需要根据实际的业务需求、硬件资源和并发访问量进行合理设置。以下是对常见配置项的详细说明和推荐值: ### server.port 这是应用监听的 HTTP 端口号,默认是 `8080`。可以根据实际部署环境修改为其他端口,例如: ```yaml server: port: 8081 ``` ### server.compression.enabled 启用 HTTP 压缩可以减少网络传输的数据量,提升响应速度。建议在高并发或带宽受限的场景下开启压缩: ```yaml server: compression: enabled: true # 可选:设置压缩的最小响应大小(单位:字节) min-response-size: 2048 # 可选:指定要压缩的 MIME 类型 mime-types: text/html,text/xml,text/plain,application/json,application/xml ``` ### server.tomcat.max-threads 该参数控制 Tomcat 的最大线程数,用于处理 HTTP 请求。默认值通常为 `200`,但在 CPU 密集型任务较多的情况下,应适当降低此值以避免线程竞争。参考经验表明,一般设置为 `200-1000` 范围内较为合适,具体需结合负载测试结果调整[^2]。 示例配置: ```yaml server: tomcat: max-threads: 500 ``` ### server.tomcat.min-spare-threads 最小空闲线程数,确保有足够线程立即响应突发请求。通常设置为 `3-10` 即可,具体取决于系统负载情况: ```yaml server: tomcat: min-spare-threads: 5 ``` ### server.tomcat.accept-count 当所有可用线程都被占用时,新连接请求将被放入等待队列中。该参数设置等待队列的最大长度,默认为 `100`。如果预期会有大量瞬时并发请求,可以适当增加此值: ```yaml server: tomcat: accept-count: 100 ``` ### server.tomcat.max-connections 最大连接数,表示 Tomcat 同时能够处理的最大连接数量。默认为 `10000`,可根据服务器性能和网络带宽进行调整: ```yaml server: tomcat: max-connections: 10000 ``` ### server.tomcat.connection-timeout 连接建立超时时间,单位为毫秒。对于高延迟网络环境,可以适当增加此值以避免连接中断: ```yaml server: tomcat: connection-timeout: 10000 ``` ### server.tomcat.max-http-request-header-size HTTP 请求头的最大大小,单位为字节。默认情况下,Spring Boot 设置为 `8KB`,若请求头较大(如包含大量 Cookie 或自定义 Header),则需增加此值以避免报错: ```yaml server: tomcat: max-http-request-header-size: 16384 # 16 KB ``` ### 完整配置示例(application.yml 格式): ```yaml server: port: 8081 compression: enabled: true min-response-size: 2048 mime-types: text/html,text/xml,text/plain,application/json,application/xml tomcat: max-threads: 500 min-spare-threads: 5 accept-count: 100 max-connections: 10000 connection-timeout: 10000 max-http-request-header-size: 16384 ``` ### 配置注意事项: - **性能调优**:建议通过压力测试工具(如 JMeter、Gatling)模拟真实业务场景,验证配置是否合理。 - **资源监控**:使用 APM 工具(如 Prometheus + Grafana)实时监控线程池、内存、CPU 使用率等指标,及时发现瓶颈。 - **日志分析**:关注 Tomcat 日志中的 `connection reset`、`too many open files` 等异常信息,优化系统级别的文件描述符限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值