HttpClient 向 Nginx 服务器投诉“NoHttpResponseException”
Posted
技术标签:
【中文标题】HttpClient 向 Nginx 服务器投诉“NoHttpResponseException”【英文标题】:HttpClient complaints 'NoHttpResponseException' with Nginx server 【发布时间】:2015-12-18 09:42:18 【问题描述】:HttpClient 投诉 'NoHttpResponseException: The target server failed to respond' with nginx server.
但是,在 Nginx 端禁用 keepalive 后(通过设置 keepalive_timeout 0),异常消失了。
这背后的原因是什么? 并且,禁用保持活动会损害性能。 有什么更好的解决方案吗?
客户端代码栈:
14:00:39.945 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG org.apache.http.headers - >> Host: blabla.SomeConsulServer.com:80
14:00:39.945 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG org.apache.http.headers - >> Connection: Keep-Alive
14:00:39.946 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG org.apache.http.headers - >> User-Agent: Apache-HttpClient/4.2.5 (java 1.5)
14:00:41.049 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection 0.0.0.0:58648<->54.236.148.255:80 closed
14:00:41.049 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG o.a.h.impl.client.DefaultHttpClient - Closing the connection.
14:00:41.050 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection 0.0.0.0:58648<->54.236.148.255:80 closed
14:00:41.050 [pool-15-thread-1-ScalaTest-running-SomeTest] INFO o.a.h.impl.client.DefaultHttpClient - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
14:00:41.059 [pool-15-thread-1-ScalaTest-running-SomeTest] DEBUG o.a.h.impl.client.DefaultHttpClient - The target server failed to respond
org.apache.http.NoHttpResponseException: The target server failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95) ~[httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62) ~[httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261) ~[httpcore-4.4.jar:4.4]
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283) ~[httpcore-4.4.jar:4.4]
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252) ~[httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191) ~[httpclient-4.2.5.jar:4.2.5]
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272) ~[httpcore-4.4.jar:4.4]
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124) ~[httpcore-4.4.jar:4.4]
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:715) [httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520) [httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) [httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1138) [httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1076) [httpclient-4.2.5.jar:4.2.5]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1050) [httpclient-4.2.5.jar:4.2.5]
at com.ecwid.consul.transport.DefaultHttpTransport.executeRequest(DefaultHttpTransport.java:71) [consul-api-1.1.6.jar:na]
at com.ecwid.consul.transport.DefaultHttpTransport.makeDeleteRequest(DefaultHttpTransport.java:66) [consul-api-1.1.6.jar:na]
at com.ecwid.consul.v1.ConsulRawClient.makeDeleteRequest(ConsulRawClient.java:87) [consul-api-1.1.6.jar:na]
at com.ecwid.consul.v1.kv.KeyValueConsulClient.deleteKVValue(KeyValueConsulClient.java:307) [consul-api-1.1.6.jar:na]
at com.ecwid.consul.v1.kv.KeyValueConsulClient.deleteKVValue(KeyValueConsulClient.java:296) [consul-api-1.1.6.jar:na]
at com.ecwid.consul.v1.ConsulClient.deleteKVValue(ConsulClient.java:461) [consul-api-1.1.6.jar:na]
at com.bla.bla.ConsulRestClient.remove(ConsulRestClient.scala:109) [classes/:na]
at com.bla.bla.SomeTest$$anonfun$6.apply$mcV$sp(SomeTest.scala:73) [test-classes/:na]
at com.bla.bla.SomeTest$$anonfun$6.apply(SomeTest.scala:66) [test-classes/:na]
at com.bla.bla.SomeTest$$anonfun$6.apply(SomeTest.scala:66) [test-classes/:na]
at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.Transformer.apply(Transformer.scala:22) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.Transformer.apply(Transformer.scala:20) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:166) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.Suite$class.withFixture(Suite.scala:1122) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuite.withFixture(FunSuite.scala:1555) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$class.invokeWithFixture$1(FunSuiteLike.scala:163) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$$anonfun$runTest$1.apply(FunSuiteLike.scala:175) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$$anonfun$runTest$1.apply(FunSuiteLike.scala:175) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$class.runTest(FunSuiteLike.scala:175) [scalatest_2.10-2.2.4.jar:na]
at com.bla.bla.SomeTest.org$scalatest$BeforeAndAfter$$super$runTest(SomeTest.scala:8) [test-classes/:na]
at org.scalatest.BeforeAndAfter$class.runTest(BeforeAndAfter.scala:200) [scalatest_2.10-2.2.4.jar:na]
at com.bla.bla.SomeTest.runTest(SomeTest.scala:8) [test-classes/:na]
at org.scalatest.FunSuiteLike$$anonfun$runTests$1.apply(FunSuiteLike.scala:208) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$$anonfun$runTests$1.apply(FunSuiteLike.scala:208) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:413) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:401) [scalatest_2.10-2.2.4.jar:na]
at scala.collection.immutable.List.foreach(List.scala:318) [scala-library-2.10.5.jar:0.13.6]
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:396) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:483) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$class.runTests(FunSuiteLike.scala:208) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuite.runTests(FunSuite.scala:1555) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.Suite$class.run(Suite.scala:1424) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuite.org$scalatest$FunSuiteLike$$super$run(FunSuite.scala:1555) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$$anonfun$run$1.apply(FunSuiteLike.scala:212) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$$anonfun$run$1.apply(FunSuiteLike.scala:212) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.SuperEngine.runImpl(Engine.scala:545) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.FunSuiteLike$class.run(FunSuiteLike.scala:212) [scalatest_2.10-2.2.4.jar:na]
at com.bla.bla.SomeTest.org$scalatest$BeforeAndAfter$$super$run(SomeTest.scala:8) [test-classes/:na]
at org.scalatest.BeforeAndAfter$class.run(BeforeAndAfter.scala:241) [scalatest_2.10-2.2.4.jar:na]
at com.bla.bla.SomeTest.run(SomeTest.scala:8) [test-classes/:na]
at org.scalatest.tools.Framework.org$scalatest$tools$Framework$$runSuite(Framework.scala:466) [scalatest_2.10-2.2.4.jar:na]
at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:677) [scalatest_2.10-2.2.4.jar:na]
at sbt.TestRunner.runTest$1(TestFramework.scala:76) [testing-0.13.6.jar:0.13.6]
at sbt.TestRunner.run(TestFramework.scala:85) [testing-0.13.6.jar:0.13.6]
at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:197) [testing-0.13.6.jar:0.13.6]
at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:197) [testing-0.13.6.jar:0.13.6]
at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:185) [testing-0.13.6.jar:0.13.6]
at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:197) [testing-0.13.6.jar:0.13.6]
at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:197) [testing-0.13.6.jar:0.13.6]
at sbt.TestFunction.apply(TestFramework.scala:202) [testing-0.13.6.jar:0.13.6]
at sbt.Tests$$anonfun$9.apply(Tests.scala:216) [actions-0.13.6.jar:0.13.6]
at sbt.Tests$$anonfun$9.apply(Tests.scala:216) [actions-0.13.6.jar:0.13.6]
at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:44) [task-system-0.13.6.jar:0.13.6]
at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:44) [task-system-0.13.6.jar:0.13.6]
at sbt.std.Transform$$anon$4.work(System.scala:63) [task-system-0.13.6.jar:0.13.6]
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226) [tasks-0.13.6.jar:0.13.6]
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226) [tasks-0.13.6.jar:0.13.6]
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17) [control-0.13.6.jar:0.13.6]
at sbt.Execute.work(Execute.scala:235) [tasks-0.13.6.jar:0.13.6]
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226) [tasks-0.13.6.jar:0.13.6]
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226) [tasks-0.13.6.jar:0.13.6]
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159) [tasks-0.13.6.jar:0.13.6]
at sbt.CompletionService$$anon$2.call(CompletionService.scala:28) [tasks-0.13.6.jar:0.13.6]
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_80]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [na:1.7.0_80]
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_80]
这里是Nginx服务器的配置:
user nginx;
worker_processes auto;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;
events
worker_connections 1024;
http
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$upstream_addr $request_time $upstream_response_time $pipe';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 0;
client_max_body_size 0;
proxy_read_timeout 900s;
proxy_send_timeout 900s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
server
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /
error_page 404 /404.html;
location = /40x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html
include /etc/nginx/conf.d/*.conf;
谢谢。
【问题讨论】:
【参考方案1】:为了在 Nginx 中通过反向代理使用 keep-alive,您需要创建一个上游。
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 "";
...
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
【讨论】:
以上是关于HttpClient 向 Nginx 服务器投诉“NoHttpResponseException”的主要内容,如果未能解决你的问题,请参考以下文章
客户端怎么使用httpclient向https服务器发送数据
从 HttpClient 向 webapi 提交 File 和 Json 数据