Using Jetty's proxy in standalone Jetty application

mnd

I'm investigating using Jetty 9 as a proxy, using standalone Jetty, not embedded Jetty. I've looked for help in many places:

Most of these are related to embedded 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. 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.

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. The page remains blank, and eventually ProxyServlet's onResponseFailure() is called with a TimeoutException, "Total timeout elapsed". Dev tools shows the request receiving a 504 Gateway Timeout.

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

<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

...
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();
    }
}
mnd

I found the hints I needed to solve this with jetty transparent proxy always returns 403 forbidden. 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.

Updated 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.
  2. Use an <init-param> of proxyTo using the address you would like to proxy the requests to. This also means that the ProxyServletExtension.java class above (in the question) is completely unnecessary.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jenkins standalone implementation: Winstone or Jetty?

Spring boot external configuration in standalone jetty

how to use Jetty's onWebSocketBinary API in a real world application

Using Websockets in Jetty

Using Elasticsearch with jetty jersey

Application using Jetty gets OutOfMemoryError on sudden burst of memory and threads

monitor the jetty based java application using Prometheus and Grafana

How to configure async timeout in embedded jetty using spring boot application

System environment variables in Jetty application

Debugging gradle web application in Jetty

Keycloak server behind Jetty reverse proxy

Configuring Jetty WebSocket Client to use proxy

How do I run nginx as proxy to jetty?

How do I configure nginx as proxy to jetty?

Configuring ha-proxy for "war" file in jetty

Using Jersey's Dependency Injection in a Standalone application

Jetty SslConnector's deprecated methods

Connection Pooling using Jetty, in Oracle

Deploying standalone war file from Spring Boot to Jetty

Vaadin 22 Starter not working in a standalone instance of Jetty v9

Using the camel jetty component with the REST DSL - "Component jetty is not a RestApiConsumerFactory" exception

Vaadin with using Jetty Server -> ERROR: Jetty server existing

Migrating Jetty to SpringBoot Jetty

Spring Boot + Tomcat + Jetty - Application failed to start

Can't run my web application on jetty

What is the general way to deploy jetty application in production?

Eclipse Jetty Server Single Instance Application

apache camel "Jetty vs Servlet" in web application

Run a Java Application with Embedded Jetty Server on Docker