2

Incoming request

How can i create a Nginx rewrite rule in the appriopriate server block, that takes any URL, that does not end on .do and add an .jsp extension, and than hand over the request to the proxied (Tomcat) server?

Outgoing request

How can i create a Nginx rewrite rule in the apprioate server block, that takes any URL received from the proxied (Tomcat) server that ends on .jsp, remove the .jsp extension, and send the response to the client?

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 when the request is received, but Nginx also sends the request to Tomcat without the .jsp extension, so tomcat does not know what to look for and returns a 404.

I have confirmed the above by looking at both Nginx and Tomcat server logs.

The problem

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, the 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.

2
  • But you have a return 302 /$1 first and there the $1 is everything without the .jsp extension. So the client gets a redirect, it comes back to your server, now in the try_files, it tests if there is $uri.jsp there is not, so it sends it whole to @proxy (without the .jsp extension) – Patrick Mevzek May 25 '18 at 19:40
  • Also is Nginx mandatory here? Besides using external parts in Perl/JS/etc and their Nginx modules I am not sure you can do such proxying. You may have more luck with Apache proxying feature. But in all cases I doubt any proxy will try one url and then another in the backend, it is not really supposed to work like that you may need to revisit your whole setup. – Patrick Mevzek May 25 '18 at 21:10
0

I think I know what you need:

location ^~ .jsp {
  rewrite /(.*).jsp /$1  break;
  proxy_pass         http://websites;
  proxy_redirect     off;
  proxy_set_header   Host $host;
}

Instead of return 302, you better off rewriting the request applying the transformation you need without the redirection.

This way you have one less roundtrip from browser to server and back again.

1
  • Can you add some explanation as to why this is the solution? – Dave M May 29 '18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.