JSF and/or PrimeFaces stylesheets and scripts go to /Project/javax.faces.resource location instead of ./javax.faces.resource JSF and/or PrimeFaces stylesheets and scripts go to /Project/javax.faces.resource location instead of ./javax.faces.resource nginx nginx

JSF and/or PrimeFaces stylesheets and scripts go to /Project/javax.faces.resource location instead of ./javax.faces.resource


Even though I have the strong impression that this problem really needs to be solved in the Ngnix proxy side, I'll explain how you could "workaround" it from the JSF side on.

JSF resources are represented by the Resource class wherein the getRequestPath() method is responsible for returning the resource URL. You could create a custom Resource implementation wherein the getRequestPath() is implemented/overridden accordingly.

public class MyResource extends ResourceWrapper {    private Resource wrapped;    public MyResource(Resource wrapped) {        this.wrapped = wrapped;     }    @Override    public String getRequestPath() {        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();        return "." + wrapped.getRequestPath().substring(contextPath.length());    }    @Override    public Resource getWrapped() {        return wrapped;    }}

Now, to override the default JSF Resource by this custom implementation, you need to create a custom ResourceHandler implementation wherein you return the custom Resource implementation instead in the createResource() method.

public class MyResourceHandler extends ResourceHandlerWrapper {    private ResourceHandler wrapped;    public MyResourceHandler(ResourceHandler wrapped) {        this.wrapped = wrapped;    }    @Override    public Resource createResource(String resourceName, String libraryName) {        return new MyResource(wrapped.createResource(resourceName, libraryName));    }    @Override    public ResourceHandler getWrapped() {        return wrapped;    }}

Finally, to get it to run, register it as <resource-handler> in the faces-config.xml.

<application>    <resource-handler>com.example.MyResourceHandler</resource-handler></application>


i am a little late with this answer, but i had the same problem and tried to do it the 'nginx' way.

So maybe this helps others out also.

you can define a second location in your nginx configuration (this surely depends on your specific configuration) which will match for a specific rule for every traffic matching the pattern for the location.

your first location in the nginx server config will look somewhat similar to this:

    location / {            proxy_set_header X-Forwarded-Host $host;            proxy_set_header X-Forwarded-Server $host;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_pass http://localhost:8080/MyProject$request_uri;    }

then you just need to add a second location for your resources, which will tell nginx to not add the MyProject in the rewrite for resources which match to the specific location pattern, e.g.:

    location /MyProject {            proxy_set_header X-Forwarded-Host $host;            proxy_set_header X-Forwarded-Server $host;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_pass http://localhost:8080$request_uri;    }

Note that the proxy_pass has changed for '/MyProject/javax.faces.resource'. Your js, images and css should now be delivered correctly again.

More about locations can be found in the nginx documentation.