22

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?

1 Answer 1

17

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.

3
  • 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 Aug 1, 2013 at 11:15
  • What do the logs on your tomcat server say?
    – Flup
    Aug 1, 2013 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. Aug 1, 2013 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .