rewrite behind a reverse proxy

Splash Forums Rewrite Users rewrite behind a reverse proxy

This topic contains 0 replies, has 1 voice, and was last updated by  Ced 1 week ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #27434

    Ced
    Participant

    I already post a question on stackoverflow here

    I’m using JSF 2.2 (Mojarra 2.2.8) and ocpsoft rewrite 2.0.12.Final In the 8.2.1 WildFly server.

    I want to rewrite my URLs. Here is my conf file :

    @RewriteConfiguration
    public class RewriteConfiguration extends HttpConfigurationProvider {
    
        @Override
        public Configuration getConfiguration(ServletContext ctx) {
            return rewrite(ConfigurationBuilder.begin(), ctx);
        }
    
        private ConfigurationBuilderRoot rewrite(ConfigurationBuilderRoot conf, ServletContext ctx) {
            conf = rewriteWiyhId(conf, ctx, "/resource-1/edit.xhtml", "/resource-1/{id}/edit");
            conf = rewriteWiyhId(conf, ctx, "/resource-1/view.xhtml", "/resource-1/{id}/view");
            conf = rewriteWiyhId(conf, ctx, "/resource-2/edit.xhtml", "/resource-2/{id}/edit");
            conf = rewriteWiyhId(conf, ctx, "/resource-2/view.xhtml", "/resource-2/{id}/view");
            return conf;
        }
    
        private ConfigurationBuilderRoot rewriteWiyhId(ConfigurationBuilderRoot conf, ServletContext ctx, String fromURL, String toURL) {
            return conf
                .addRule()
                    .when(Direction.isInbound()
                        .and(Path.matches(fromURL)
                        .and(Query.parameterExists("id"))))
                    .perform(Redirect.permanent(ctx.getContextPath() + toURL))
                .addRule(Join.path(toURL).to(fromURL));
        }
    
        @Override
        public int priority() {
            return 10;
        }
    }

    The point is to redirect URLs that match a pattern and have an id : /resource-1/view.xhtml?id=42 to /app/resource-1/42/view (I have to add the contextPath to redirect). And then join the URL /resource-1/42/view to the file /resource-1/view.xhtml?id=42.

    And this is working in dev, on my CPU but not in test nor in prod on a server. The URL are not redirected : they remain /resource-1/view.xhtml?id=42 and if I enter the url /app/resource-1/42/view I have a “Not found” page

    Well this not working when I use the domain name :

    http://domain.com/app/resource-1/42/view

    but if I try with the IP address :

    http://123.123.123.123:8080/app/resource-1/42/view

    it’s working…

    So I suspect a problem due to Apache reverse proxy, in fact the URL domain.com is linked to a server while the URL domain.com/app is linked to another server. the proxy conf is :

    ProxyPass /app http://123.123.123.123:8080/app/
    ProxyPassReverse /app/ http://123.123.123.123:8080/app/
    ProxyPassReverseCookiePath /app/ http://123.123.123.123:8080/app/

    Is there something I can do to make the rewriting working? Changing the proxy conf or the ocpsoft rewrite conf?

    EDIT

    On a server (test or prod)

    • when I redirect in JSF using FacesContext.getCurrentInstance().getExternalContext().redirect(), the ocpsoft-rewrite doesn’t perform the redirect so the url remain resource-1/view.xhtml?id=42.
    • when I navigate returning an outcome : /resource-1/view.xhtml?faces-redirect=true&includeViewParams=true&id=42 in the action param of a commandButton, the ocpsoft-rewrite does perform the redirect so the URL is now resource-1/42/view but that URL is not join so I have a “not found” page.

    I tried to redirect with absolute path. I use a JNDI resource that indicate the JSF ProjectStage I previously setup. I add the rewrite-integration-cdi artifact in the pom.xml.

    @RewriteConfiguration
    @Slf4j
    public class RewriteConfiguration extends HttpConfigurationProvider {
    
        @Resource(lookup = "java:/env/jsf/ProjectStage")
        private String projectStage;
    
        @Override
        public Configuration getConfiguration(ServletContext ctx) {
            log.info("getConfiguration() start ContextPath : {}, {}", ctx.getContextPath(), projectStage);
            return rewrite(ConfigurationBuilder.begin(), ctx);
        }
    
        private ConfigurationBuilderRoot rewrite(ConfigurationBuilderRoot conf, ServletContext ctx) {
            conf = rewriteWiyhId(conf, ctx, "/resource-1/edit.xhtml", "/resource-1/{id}/edit");
            conf = rewriteWiyhId(conf, ctx, "/resource-1/view.xhtml", "/resource-1/{id}/view");
            conf = rewriteWiyhId(conf, ctx, "/resource-2/edit.xhtml", "/resource-2/{id}/edit");
            conf = rewriteWiyhId(conf, ctx, "/resource-2/view.xhtml", "/resource-2/{id}/view");
            return conf;
        }
    
        private ConfigurationBuilderRoot rewriteWiyhId(ConfigurationBuilderRoot conf, ServletContext ctx, String fromURL, String toURL) {
            return conf
                .addRule()
                    .when(Direction.isInbound()
                        .and(Path.matches(fromURL)
                        .and(Query.parameterExists("id"))))
                    .perform(Redirect.permanent(getDomain() + ctx.getContextPath() + toURL))
                .addRule(Join.path(toURL).to(fromURL));
        }
    
        private String getDomain() {
            switch (ProjectStage.valueOf(projectStage)) {
                case Development: return "http://localhost:8080";
                case SystemTest: return "http://test.domain.com";
                case Production: return "http://domain.com";
                default: throw new IllegalStateException("stage " + projectStage + " unexpected");
            }
        }
    
        @Override
        public int priority() {
            return 10;
        }
    }

    Again this is working in dev but not in test (in a server behind a revers proxy)

    Maybe the projectstage solution will work if I use the IP address but that doesn’t sound to be a good idea to deal with IP (even if I’m in a standalone mode).

    The log I add in the getConfiguration() methoh is called in test and in prod.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Comments are closed.