nginx send_timeout 超时导致的302 错误

来源:互联网 发布:数据采集有哪几种接头 编辑:程序博客网 时间:2024/05/21 07:10

背景:

 前端fe js轮询(每隔5s) 一个web接口


php-fpm配置:

pm=static

pm.max_children=128


 nginx 配置:

 error_page 400 403 404 500 501 502 503 504 505 http://www.baidu.com/search/error.html?tc=test;

    fastcgi_connect_timeout 5;
    fastcgi_send_timeout 10; 
    fastcgi_read_timeout 10; 
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_intercept_errors on; 
    keepalive_timeout  0;  

nginx错误日志( error_log):


谷歌浏览器错误页面:

随着请求的增多,耗时请求增多,当页面耗时超过10s 则返回302 跳转到 http://www.baidu.com/search/error.html?tc=test; 


原因分析:

php脚步 执行超过了10s.导致nginx 等待超时send_timeout ,504,然后error_page 返回浏览302 错误页面


502 504 错误可参照本人之前的 502 504 错误的区别

502  nginx发起连接到php-fpm(由于php-fpm队列太小 被拒绝) 或者 php 脚本超时 直接断开于nginx的链接

504 nginx 超时时间太短(且php-fpm队列较大) 请求太多导致








1. send_timeout

syntax: send_timeout the time

default: send_timeout 60

context: http, server, location

Directive assigns response timeout to client. Timeout is established not on entire transfer of answer, but only between two operations of reading, if after this time client will take nothing, then nginx is shutting down the connection.




注意fastcgi_connect_timeout fastcgi_read_timeout fastcgi_send_timeout 类似


proxy_connect_timeout 65s; #连接超时 默认为60秒
proxy_read_timeout 65s; #读取超时 默认为60秒
proxy_send_timeout 65s; #发送超时 默认为60秒

 

Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location 
Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.
定义用于建立与代理服务器的连接超时。应当注意的是,此超时通常不能超过75秒。

 

Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.
定义了从代理服务器读取响应超时。只是在两个连续的读取操作之间设置超时,而不是为整个响应的传输设置超时时间。如果代理服务器在这段时间内不发送任何东西,连接关闭。

 

Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
Sets a timeout for transmitting a request to the proxied server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed.

设置发送到代理服务器的请求的超时。只是在两个连续的写操作之间设置超时,而不是为整个请求的传输设置超时时间。如果代理服务器在这段时间内没有收到任何东西,连接关闭。


nginx比较强大,可以针对单个域名请求做出单个连接超时的配置. 

比如些动态解释和静态解释可以根据业务的需求配置

proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout:连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

 

 

server
  {
 listen       80;
 server_name www.qq.cn;
 index index.jsp index.do index.html;
 root  /data/webapp/qqroot;

 #limit_conn   crawler  20;

 location /(WEB-INF)/ {
  deny all;
 }

 location / {
  proxy_pass http://192.168.1.31:8081;
  proxy_connect_timeout 500s;
  proxy_read_timeout 500s;
  proxy_send_timeout 500s;
  proxy_set_header        X-Real-IP $remote_addr;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header        Host $http_host;
 }

 location ~* (\.jpg)|(\.gif)|(\.png)|(\.swf)|(\.html)|(\.htm)|(\.exe)|(\.flv)|(\.doc)|(\.rar)|(\.rtf)|(\.bmp)|(\.xls)$
 {
  root /data/webapp/qqroot/;
 }

 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 {
  expires      30d;
 }


 }


proxy_connect_timeout

语法: proxy_connect_timeout timeout_in_seconds

上下文: http, server, location

This directive assigns a timeout for the connection to the proxyserver. This is not the time until the server returns the pages, this is the [#proxy_read_timeout proxy_read_timeout] statement. If your proxyserver is up, but hanging (e.g. it does not have enough threads to process your request so it puts you in the pool of connections to deal with later), then this statement will not help as the connection to the server has been made. It is necessary to keep in mind that this time out cannot be more than 75 seconds.

 

proxy_read_timeout

语法: proxy_read_timeout the_time

默认值: proxy_read_timeout 60

上下文: http, server, location

This directive sets the read timeout for the response of the proxied server. It determines how long NGINX will wait to get the response to a request. The timeout is established not for entire response, but only between two operations of reading.

In contrast to [#proxy_connect_timeout proxy_connect_timeout] , this timeout will catch a server that puts you in it's connection pool but does not respond to you with anything beyond that. Be careful though not to set this too low, as your proxyserver might take a longer time to respond to requests on purpose (e.g. when serving you a report page that takes some time to compute). You are able though to have a different setting per location, which enables you to have a higher proxy_read_timeout for the report page's location.

If the proxied server nothing will communicate after this time, then nginx is shut connection.

 

proxy_send_timeout

语法: proxy_send_timeout time_in_seconds

默认值: proxy_send_timeout 60

上下文: http, server, location

This directive assigns timeout with the transfer of request to the proxy server. Time out is established not on entire transfer of request, but only between two operations of record. If after this time the proxy server will not take new data, then nginx is shut the connection



proxy_connect_timeout、proxy_read_timeout、proxy_send_timeout

 

proxy_connect_timeout  是和后端建立连接的超时时间,记住不要超过 75s 。如果超时,Nginx 的返回码是多少? 504

proxy_read_timeout  是从后端读取数据的超时时间,两次读取操作的时间间隔如果大于这个值,和后端的连接会被关闭。如果一个请求时间时间非常大,要把这个值设大点。如果超时,Nginx 的返回码是多少? 504

proxy_send_timeout  是向后端写数据的超时时间,两次写操作的时间间隔大于这个值,也就是过了这么长时间后端还是没有收到数据(我理解是 根据 TCP ACK 判断),连接会被关闭。如果超时,Nginx 的返回码是多少? 504



0 0