环境是Nginx + uwsgi.
在某些GET请求中从Nginx获取502错误的网关错误.似乎与URL的长度有关.在我们的特定情况下,它是一长串的GET参数.缩短GET参数,没有502错误.
来自nginx/error.log
[error] 22113#0: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.1.100, server: server.domain.com, request: "GET <long_url_here>"
Run Code Online (Sandbox Code Playgroud)
uwsgi错误日志中没有信息.
我正在为我的应用程序运行负载测试.我有两个服务器:一个用我的应用程序和一个虚拟服务器负责让我回复.
在我的虚拟服务器中,我有以下jsp代码:
<%@ page import="java.util.Random" %>
<%@ page language="java" %>
<%@ page session="false" %>
<%
String retVal = "some json string";
Thread.sleep(50);
%>
Run Code Online (Sandbox Code Playgroud)
我正在使用tomcat7运行应用程序.我的server.xml连接池(在两个服务器中)看起来像:
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="1500" minSpareThreads="1000" prestartminSpareThreads="true" />
<Connector port="9031" protocol="HTTP/1.1"
connectionTimeout="20000"
maxConnections="4000"
executor="tomcatThreadPool"
redirectPort="8443" />
Run Code Online (Sandbox Code Playgroud)
我从服务器运行的java代码是:
HttpPost post = new HttpPost(bidderUrl);
post.setHeader("Content-Type", "application/json");
// I'm using http client with ThreadSafeClientConnManager
// total conn = 500, max conn per route = 100, timeout=500millis
HttpClient httpClient = httpClientFactory.getHttpClient();
try {
post.setEntity(new StringEntity(jsobBidRequest));
HttpResponse response = httpClient.execute(post);
...
catch …Run Code Online (Sandbox Code Playgroud) 我正在使用apache http客户端来测试我的WS.我在球衣上写了一个WS.这个WS的URL是
http://localhost:8080/mobilestore/rest/sysgestockmobilews/getinventory?xml=dataString
Run Code Online (Sandbox Code Playgroud)
使用url调用此WS我已经编写了一个方法,如下所示
public static void getInventory(String input)
throws ClientProtocolException, IOException {
System.out.println(input);
String url = URL + "getinventory";
HttpClient client = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("xml", input));
String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
url += "?" + paramString;
System.out.println(url);
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行程序并将url传递给此函数时,我在行中获得异常
HttpResponse response = client.execute(request);
Run Code Online (Sandbox Code Playgroud)
例外情况如下 …
我们有上面的配置,其中服务器最多处理 100 个并发请求,其中一些请求相当大:单个请求最多 3-4MB 有效负载(不使用多部分等)
这工作正常,但有时我们会从 nginx 收到错误:
HTTP 502 "Bad Gateway"
在访问日志中我们看到
writev() failed (104: Connection reset by peer) while sending request to upstream
或者:recv() failed (104: Connection reset by peer) while reading response header from upstream
Java(Jetty/Dropwizard)服务器上没有错误。
我们假设这是由于 Jetty 关闭连接太快了,甚至可能在接收数据完成之前就发送了响应?!
我们尝试增加 nginx 上的缓冲区大小:
client_max_body_size 24M;
client_body_buffer_size 128k;
并降低 keep_alive 空闲超时(60 -> 25 秒)以避免码头提前关闭它的情况(30 秒超时):
proxy_http_version 1.1;
proxy_set_header Connection "";
keepalive_timeout 25;
这并没有带来改变。标题不应该太大。99.9% 的请求按预期工作(所有请求都是相似的)。
奇怪的是,升级 Dropwizard (1.2.2 -> 1.3.5) 和 Jetty (9.4.7 -> 9.4.11) 后,错误发生的频率增加了 20 倍。
我想知道下一步调查应该做什么?可能发生了什么/我们可以尝试解决方法的陷阱是什么?
我正在使用 apache httpclient 4.4 来做 https 请求,我可以不断地看到 org.apache.http.NoHttpResponseException.
这是我创建 httpclient 的代码。
TrustManager[] trustAllCerts =
new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
}
} };
SSLContext sslcontext = null;
try {
sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
logger.warn(e.getMessage());
} catch (KeyManagementException e) {
logger.warn(e.getMessage());
}
// Allow TLSv1 protocol only …Run Code Online (Sandbox Code Playgroud) java ×3
nginx ×2
apache ×1
django ×1
dropwizard ×1
http ×1
httpclient ×1
jersey ×1
jetty ×1
tomcat ×1
uwsgi ×1
web-services ×1