jenkins在使用maven部署到nginx+tomcat服务器时出现504异常

jenkins在使用maven部署到nginx+tomcat服务器时出现504错误

环境:jenkins+maven+nginx+tomcat

在执行jenkins job发布项目到远程服务器,服务器使用nginx+tomcat。默认使用80端口,通过nginx转发请求。在执行tomcat:redeploy时,出现下面的错误:

[ERROR] Failed toexecute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:redeploy (default-cli)on project sgt-node: Cannot invoke Tomcat manager: Server returned HTTPresponse code: 504 for URL: http://host/manager/text/deploy?path=%2Fnode&war=&update=true-> [Help 1]
[ERROR]
[ERROR] To see thefull stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Mavenusing the -X switch to enable full debug logging.
[ERROR]
[ERROR] For moreinformation about the errors and possible solutions, please read the followingarticles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

但项目又是部署成功了的,只是jenkins的job执行失败。刚开始一直以为是tomcat的连接超时了,设置connectionTimeout为-1时(keepAliveTimeout默认使用connectionTimeout的值),即永不超时,错误依旧。那是不是nginx连接的超时了呢?所以设置了nginx的keepalive_timeout参数,但还是没有效果。

后来经过多番查找资料(对nginx不熟),查看到nginx的错误日志(error_log),发现下面错误:

2014/05/16 11:23:03[error] 4789#0: *36073 upstream timed out (110: Connection timed out) whilereading response header from upstream, client: 172.16.77.102, server:172.16.73.53, request: "PUT/manager/text/deploy?path=%2Fnode&war=&update=true HTTP/1.0",upstream:"http://127.0.0.1:8080/manager/text/deploy?path=%2Fnode&war=&update=true",host: "host"

这是nginx连接超时,而由于使用的是80端口,走的是nginx代理转发的请求,原来,在nginx转发请求后,等待读取响应的时间超时的情况下,nginx直接返回了一个504 Gateway Time-out错误,所以会出现部署成功,却返回错误的情况。

知道了原因后就好办了,查了一下资料,设置nginx代理的读取超时时间参数:

proxy_read_timeout 600s;

再试,果然成功了。问题解决。


另外,如果不使用nginx代理转发请求,也可避免这个问题,直接使用http://host:port/manager/text,只要不使用nginx默认的80端口即可。