How to use ProxyServlet

994 views
Skip to first unread message

Martin M

unread,
Aug 11, 2016, 7:20:31 AM8/11/16
to dropwizard-user
After many attempts at making dropwizard proxy all i get is status code 502 Bad Gateway:

I have and old jetty project on http://test:80/

and a new dropwizard project on http://dropwizard:8085/

I want the old project to be proxied on  http://dropwizard:8085/old/

I have tried:


        ProxyClassic proxyClassic = new ProxyClassic("http://test:80/", "/old");
        environment.servlets().addServlet("ProxyClassic", proxyClassic).addMapping("/old/*");

public class ProxyClassic extends ProxyServlet {

    public ProxyClassic(String proxyTo, String proxyPrefix) {
        delegate._proxyTo = proxyTo;
        delegate._prefix = proxyPrefix;
    }
    
    private final TransparentDelegate delegate = new TransparentDelegate(this);

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        delegate.init(config);
    }

    protected static class TransparentDelegate
    {
        private final ProxyServlet proxyServlet;
        private String _proxyTo;
        private String _prefix;

        protected TransparentDelegate(ProxyServlet proxyServlet)
        {
            this.proxyServlet = proxyServlet;
        }

        protected void init(ServletConfig config) throws ServletException
        {
            // Adjust prefix value to account for context path
            String contextPath = config.getServletContext().getContextPath();
            _prefix = _prefix == null ? contextPath : (contextPath + _prefix);
        }
    }



        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-proxy</artifactId>
            <version>9.3.9.v20160517</version>
            <scope>compile</scope>
        </dependency>

I am using dropwizard 1.0.0

Any suggestions how i can do this?

Vyacheslav Rusakov

unread,
Aug 11, 2016, 8:57:38 AM8/11/16
to dropwizard-user
You can use jetty Transparent servlet (the same as your ProxyClassic):

final ServletRegistration.Dynamic proxy = environment.servlets().addServlet("proxy", ProxyServlet.Transparent.class);
proxy
.setInitParameter("proxyTo", "http://test:80/");
proxy
.setInitParameter("prefix", "/old");
proxy
.addMapping("/old/*");



четверг, 11 августа 2016 г., 18:20:31 UTC+7 пользователь Martin M написал:

Martin M

unread,
Aug 11, 2016, 11:44:12 AM8/11/16
to dropwizard-user
Just what i needed. thx

venkatesh krishnamurthy

unread,
May 24, 2017, 7:47:35 PM5/24/17
to dropwizard-user
worked.  Thanks a lot. Migrated from 0.7.1 to 1.1.0
-venkatesh

nis...@ezcred.in

unread,
Aug 25, 2017, 3:50:11 AM8/25/17
to dropwizard-user
This is very helpful. Can you help me finding the way to redirect from http to https endpoint ?
Reply all
Reply to author
Forward
0 new messages