HttpClient投诉' NoHttpResponseException:目标服务器无法响应'使用Nginx服务器。
但是,在Nginx端禁用keep alive之后(通过设置keepalive_timeout 0),异常就消失了。
这背后的原因是什么? 并且,禁用keep alive会伤害性能。 有什么更好的解决方案吗?
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;
}
感谢。
答案 0 :(得分:0)
为了在Nginx中使用反向代理保持活动,您需要创建一个上游。
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