23

I need to create a quite simple map in Nginx redirecting a subpath to another server that is located in the same subnet.

  • Nginx server: 192.168.0.2
  • Tomcat server: 192.168.0.3:8443

I tried to put this in the server section

    location /tomcatapi/ {
        rewrite /tomcatapi/(.*) $1 break;
        proxy_pass http://192.168.0.3:8443;
    }

but all I get accessing http://www.myservice.com/tomcatapi/ is a 500 error page and in nginx log file I have this error:

    the rewritten URI has a zero length

What I am missing in this conf?

carlo.polisini
  • 335
  • 1
  • 3
  • 7

1 Answers1

18

Let's look at your rewrite line:

rewrite /tomcatapi/(.*) $1 break;

You're taking the bit in brackets (i.e. everything after /tomcatapi/), which gets assigned to $1, and using that as the sole contents of your rewritten URI.

In your example, there is nothing after /tomcatapi/, so the rewrite ends up empty, and this is what nginx is moaning about.

If you change the rewrite rule to

rewrite /tomcatapi/(.*) /$1 break;

then you'll always end up with at least / in the rewrite output.

Flup
  • 7,978
  • 2
  • 32
  • 43
  • Thanks for you reply, anyway I added the "/" but now accessing the url the browser is stuck in loading state and in the log file I see now a different error: upstream sent no valid HTTP/1.0 header while reading response header from upstream – carlo.polisini Aug 01 '13 at 11:15
  • What do the logs on your tomcat server say? – Flup Aug 01 '13 at 12:53
  • I found the error, the mistake was that the app was available only at https and not http! So syntax was right after your correction, just replaced http with https to getting it work. – carlo.polisini Aug 01 '13 at 14:03