
不懂 http 协议,连 nginx 为什么这么协议都不懂
在 nginx 中,proxy_pass 是我们做内部转发时很常见的命令,同时少不了用proxy_set_header设置转发时的头部。细看文档,你会发现 nginx 会默认为你转发时设置两个头部:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;你能想到为什么这两个头部 nginx 为什么要做默认设置吗?
$proxy_host查查文档很容易理解,就是转发的目标服务器。
$proxy_host
name and port of a proxied server as specified in the proxy_pass directive;
设置 Host 头也很容易理解——如果你想了一会很想不到,那你真的该好好看看 http 协议的基础知识了。
想完了吗?答案就是:通常服务器都会设置并根据虚拟域名来过滤请求,如 nginx 中的server_name ,目的就是一台服务器,可以同时服务作用于多个域名的请求。
- Connection 头(header) 决定当前的事务完成后,是否会关闭网络连接。
如果该值是 “keep-alive”,网络连接就是持久的,不会关闭,使得对同一个服务器的请求可以继续在该连接上完成。
如果该值是 “close”,表明客户端或服务器在完结请求后想要关闭该网络连接,这是 HTTP/1.0 请求的默认值
而 HTTP 的RFC定义了:
HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.
HTTP 1.1 中,如果不支持持久链接的话,每条信息必须包含 close。因为 nginx 不知道你proxy pass过去的应用支持长连接不啊,所以只能给你先设个 close。e
proxy_pass 支持长连接
而在 proxy_pass支持长连接,更是家常便饭了。
nginx使用upstream做负载均衡时,为了提高性能,需要用keep-alive来建造连接池,复用已经创建的连接。
我记得以前看到 nginx 官方文档中,是提供了做法的:
For HTTP, the proxy_http_version directive should be set to “1.1” and the “Connection” header field should be cleared:
upstream http_backend {
server 127.0.0.1:8080;
keepalive 16;
}
server {
...
location /http/ {
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
...
}
}
Alternatively, HTTP/1.0 persistent connections can be used by passing the “Connection: Keep-Alive” header field to an upstream server, though this method is not recommended.
又到我们的思考时间了,为什么proxy_http_version要设为1.1呢?为什么这次Connection头部设置为空字符串呢?
第一个问题很容易解答,只要想一想 http 1.1与1.0的区别即可:http 1.1开始,支持keep-alive长连接。
第二个问题,其实设置为空字符串,只是为了避免别人传了 Connection为 close 的请求过来,然后我们 转发到 upstream 时 把此头部带过去,导致了全部 upstream中的长连接都被关掉——也就是keepalive设置来一点用都没有了。
也可以设置为“Connection: Keep-Alive”,但官方说不推荐,我还没有想通。
本文探讨了Nginx中proxy_pass与proxy_set_header的作用,解释了为何Nginx默认设置Host头为$proxy_host及Connection头为close的原因。深入讨论了如何利用HTTP/1.1特性提高Nginx性能,尤其是在保持长连接方面。
1329

被折叠的 条评论
为什么被折叠?



