关于Nginx的坑

By Gavin     @2019-12-08     1192 views

upstream错误

upstream sent more data than specified in “Content-Length” header while reading upstream

没有发现报错影响请求的情况,可以通过设置 proxy_buffering 为off来修复

proxy_buffering off;

判断GET参数

使用if else break等操作可以判断GET参数$query_string,例如:

location = /api {
    if ( $query_string ~ ^product=70001&version=1&user=b4(.*)$ ) {
    	# 在if内或者limit_except内的proxy_pass不能带uri
        proxy_pass http://example.com;
        break;
    }
    proxy_pass http://example.com/hello;
}

nginx log中有多个upstream_response_time和upstream_addr

  • 现象

nginx log中出现两个或者多个逗号分隔的upstream_response_time和upstream_addr,说明upstream到两个server上了

我遇到的是有两个第一个超时,第二个正常

If an error occurs during communication with a server, the request will be passed to the next server, and so on until all of the functioning servers will be tried. If a successful response could not be obtained from any of the servers, the client will receive the result of the communication with the last server.

  • 分析

也就是说,nginx会在第一个server失败的时候,请求下一个server,所以会有两个或者多个,其实只有最后一个成功了

Gavin's Daily

© 2019-2020 Gavin's GoBlog