![后端从 Jetty 8 升级到 9 后出现 Bad Gateway 错误](https://linux22.com/image/707753/%E5%90%8E%E7%AB%AF%E4%BB%8E%20Jetty%208%20%E5%8D%87%E7%BA%A7%E5%88%B0%209%20%E5%90%8E%E5%87%BA%E7%8E%B0%20Bad%20Gateway%20%E9%94%99%E8%AF%AF.png)
更新:这似乎是由于 Apache 错误造成的:https://bz.apache.org/bugzilla/show_bug.cgi?id=60330
我有一个 Java 应用程序,它运行 Jetty 8 作为嵌入式 HTTP 服务器。这是一项后端服务,运行在端口 20000 上。我的实际服务的代码与此无关。我在 GitHub 上创建了一个示例项目来演示该问题:https://github.com/janvanmansum/apache-jetty9-502-error
主要代码如下所示,供参考。它依赖于几个库:
javax.servlet:javax.servlet-api:3.1.0
commons-io:commons-io:2.5
org.eclipse.jetty:jetty-server:9.4.6.v20170531 or 8.2.0.v20160908
org.eclipse.jetty:jetty-servlet:9.4.6.v20170531 or 8.2.0.v20160908
后端服务器.java
package nl.knaw.dans.test;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
public class BackendServer {
public static void main(String[] args) throws Exception {
Server server = new Server(20000);
ServletContextHandler handler = new ServletContextHandler();
handler.addServlet(AcceptAllServlet.class, "/acceptAll");
handler.addServlet(ReadAllServlet.class, "/readAll");
server.setHandler(handler);
server.start();
server.join();
}
}
AcceptAllServlet
package nl.knaw.dans.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class AcceptAllServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("AcceptAll Servlet OK!");
resp.setStatus(200);
}
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(201);
}
}
读取所有 Servlet
package nl.knaw.dans.test;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
public class ReadAllServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("ReadAll Servlet OK!");
resp.setStatus(200);
}
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
FileOutputStream fos = new FileOutputStream("target/outfile");
try {
IOUtils.copy(req.getInputStream(), fos);
}
finally {
fos.close();
}
resp.setStatus(201);
}
}
该服务通过作为代理的 Apache HTTP Server 向外界公开,使用以下配置:
<VirtualHost *:80>
ProxyTimeout 600
<Location / >
ProxyPass http://localhost:20000/
ProxyPassReverse http://localhost:20000/
</Location>
</VirtualHost>
POST
-使用 Apache 将文件发送到后端服务对于 Jetty 8 来说工作正常,文件大小不限。但是在 Jetty 9 中,502 Bad Gateway
当我将文件发布到 AcceptAllServlet 并且文件大小超过 256K 时,我收到错误。我猜想这与 Apache 尚未完成将数据写入后端服务有关。但是,如果后端服务器想要响应,401 Unauthorized
我不希望它首先读取它将要拒绝的所有数据。
你知道为什么这种情况显然发生了变化吗?我是否错误配置了 Apache,或者我错误地使用了 Jetty?或者这是一个错误?
谢谢你的帮助!