#StackBounty: #nginx #proxy #tomcat #rewrite Nginx rewrite .jsp extension and proxy to tomcat

Bounty: 100

Outgoing

How can i create a Nginx rewrite rule in the appropriate server block, that takes any URL ending on .jsp and removes the .jsp extension after retrieving the correct .jsp page from the tomcat server, but before sending the response to the client?

Incoming

How can i create a Nginx rewrite rule in the appropriate server block, that takes any URL that does not end on .do and add a .jsp extension, after receiving a HTTP request, but before fetching the .jsp file from the tomcat server. And than follow the outgoing rewrite rule to remove the extension again before sending response?

Test

I tried to play around with the following

server {
        listen 443 ssl;
        server_name www.test.local test.local;

        location / {
                if ($request_uri ~ ^/(.*).jsp$) {
                        return 302 /$1;
                }
                try_files $uri.jsp @proxy;
        }

        location @proxy {
                proxy_pass http://websites/;
                include proxy_params;
        }
}

Nginx removes the .jsp extension, but it also sends the request to Tomcat without the .jsp extension, so tomcat does not know what to look for and returns a 404.

As far as i can tell, Nginx is not asking Tomcat do you have a $uri.jsp page but is instead asking if tomcat has a $uri page (without .jsp extension).

As far as i can read and understand try_files syntax is

try_files [Location[file, folder]] [fallback[file, folder, HTTP code]]

But the official documentation does not say (as far as i can find) how to instruct Nginx to (in this case) ask the proxy for the different files and folders to try, but is instead quering its own local root location for $uri.jsp and than using @proxy as fallback.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.