简体   繁体   English

在独立的Jetty应用程序中使用Jetty的代理

[英]Using Jetty's proxy in standalone Jetty application

I'm investigating using Jetty 9 as a proxy, using standalone Jetty, not embedded Jetty. 我正在调查使用Jetty 9作为代理,使用独立的Jetty,而不是嵌入式Jetty。 I've looked for help in many places: 我在很多地方寻求帮助:

Most of these are related to embedded Jetty: 其中大部分都与嵌入式Jetty有关:

This question is along the same lines: 这个问题也是一致的:

...but the only answer is a link to a page that covers some parameters for the proxy, but no examples or other helpful hints. ...但唯一的答案是指向代表某些参数的页面的链接,但没有示例或其他有用的提示。

On to the question... 关于这个问题......

I've created an extension to Jetty's ProxyServlet, which overrides the rewriteURI() method to actually change the request to a different URL. 我已经为Jetty的ProxyServlet创建了一个扩展,它覆盖了rewriteURI()方法,实际上将请求更改为不同的URL。 This custom proxy works when running Jetty embedded, but when I use a web.xml file and the jetty-maven-plugin to create a war to deploy, it no longer works. 这个自定义代理在运行Jetty嵌入时工作,但是当我使用web.xml文件和jetty-maven-plugin来创建要部署的战争时,它就不再有效了。

When I make a request, I can debug the application and see that it gets into the rewriteURI() method, it then calls Jetty's ProxyServlet's service() method, which runs to completion, but then nothing happens. 当我发出请求时,我可以调试应用程序并看到它进入rewriteURI()方法,然后调用Jetty的ProxyServlet的service()方法,该方法运行完成,但没有任何反应。 The page remains blank, and eventually ProxyServlet's onResponseFailure() is called with a TimeoutException, "Total timeout elapsed". 页面保持空白,最终使用TimeoutException调用ProxyServlet的onResponseFailure() ,“超时总时间”。 Dev tools shows the request receiving a 504 Gateway Timeout. 开发工具显示请求接收504网关超时。

It seems as though something is missing in how things are connected, but I can't tell what it might be. 似乎事物的连接方式缺少某些东西,但我无法分辨它可能是什么。 Any help would be greatly appreciated. 任何帮助将不胜感激。 I've included web.xml and the custom proxy ( ProxyServletExtension ) below. 我在下面包含了web.xml和自定义代理( ProxyServletExtension )。

web.xml web.xml中

<servlet>
    <servlet-name>proxy</servlet-name>
    <servlet-class>org.example.ProxyServletExtension</servlet-class>
    <init-param>
        <param-name>maxThreads</param-name>
        <param-value>1</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>proxy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

ProxyServletExtension.java ProxyServletExtension.java

...
import org.eclipse.jetty.proxy.ProxyServlet;
...
public class ProxyServletExtension extends ProxyServlet {
    @Override
    protected URI rewriteURI(HttpServletRequest request) {
        // Forward all requests to another port on this machine
        String uri = "http://localhost:8060";

        // Take the current path and append it to the new url
        String path = request.getRequestURI();
        uri += path;

        // Add query params
        String query = request.getQueryString();
        if (query != null && query.length() > 0) {
            uri += "?" + query;
        }
        return URI.create(uri).normalize();
    }
}

I found the hints I needed to solve this with jetty transparent proxy always returns 403 forbidden . 我发现使用jetty透明代理解决这个问题的提示始终返回403禁止 The question didn't exactly pertain to my question, but the code snippet provided showed me what I needed in the <servlet> in web.xml . 问题并不完全与我的问题有关,但提供的代码片段向我展示了我在web.xml中的<servlet>中所需要的内容。

Updated web.xml 更新了web.xml

<servlet>
    <servlet-name>proxy</servlet-name>
    <servlet-class>org.example.ProxyServletExtension$Transparent</servlet-class>
    <init-param>
        <param-name>maxThreads</param-name>
        <param-value>1</param-value>
    </init-param>
    <init-param>
        <param-name>proxyTo</param-name>
        <param-value>http://localhost:8060</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>proxy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Required changes 所需的更改

  1. Set the <servlet-class> to ProxyServletExtension$Transparent , previously I wasn't using a trasparent proxy. <servlet-class>ProxyServletExtension$Transparent ,之前我没有使用trasparent代理。
  2. Use an <init-param> of proxyTo using the address you would like to proxy the requests to. 使用proxyTo<init-param> ,使用您希望代理请求的地址。 This also means that the ProxyServletExtension.java class above (in the question) is completely unnecessary. 这也意味着上面的(在问题中) ProxyServletExtension.java类是完全没必要的。

Also, it is worth mentioning that there is a prefix <init-param> as well, which can be used to remove part of the incoming request before proxying to the proxied request. 此外,值得一提的是,还有一个prefix <init-param> ,它可用于在代理代理请求之前删除部分传入请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM