2

I'm trying to use nginx as a reverse proxy for two Tomcat Instances, each in their own VM. The problem is: when i start to add a folder-path to the proxy_pass argument, i get a 310 error: Too many redirects.

What am i doing wrong? Any advice is appreciated. The first server just works fine, but as mentioned before, the second, with the added folderpath wont work.

Here is my nginx config:

server {
  listen 80;
  server_name oc.domain.tld;
  location / {

       proxy_pass http://172.16.81.73;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}

server {
  listen 80;
  server_name test.domain.tld;
  location / {
       proxy_pass http://172.16.75.99/OpenClinica/;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

server {
   listen       80  default_server;
   server_name  _;
   return       444;
}

edit: there is no SSL enabled at the moment (both tomcat & nginx)

edit2: my rewrite log is empty (if i switch it on, debugging on notice level)
I just discovered this lines in my nginx log(the GET .../login/login line is repeated for about 20 times:

190.215.166.212 - - [04/May/2013:22:29:21 -0400] "GET /OpenClinica/pages/login/login HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
190.215.166.212 - - [04/May/2013:22:29:21 -0400] "GET /OpenClinica/pages/login/login HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
190.215.166.212 - - [04/May/2013:22:29:25 -0400] "-" 400 0 "-" "-"
190.215.166.212 - - [04/May/2013:22:29:25 -0400] "-" 400 0 "-" "-"

It seems, that nginx requests ....login/login in a loop. So the problem is perhaps on the tomcat side?

Patrick Werner
  • 1,106
  • 9
  • 24
  • Not sure if it's related but your catch-all server should be first rather than last, doesn't need to have a server_name set, and should return the standard 404 error, not the weird custom 444 error which is not a standard code. – Danack May 03 '13 at 19:31
  • @Danack removing the default server lines doesn't change the behavior, but thx for your comment. – Patrick Werner May 05 '13 at 02:43

1 Answers1

2

I think what is likely to be happening is that the redirect is happening on the Tomcat side, rather than the Nginx side, so I think you may not have given us enough information to figure this out.

However you can investigate this problem easily by enabling the rewrite log for Nginx by adding:

rewrite_log on;

You would then be able to see exactly what it is re-writing and if/where it is getting into a loop, if it's in the Nginx side.

EDIT

I just discovered this lines in my nginx log(the GET .../login/login line is repeated for about 20 times: GET /OpenClinica/pages/login/login GET /OpenClinica/pages/login/login

That definitely sounds like the redirect is occurring purely inside Tomcat rather than it being an internal Nginx redirect as the requests are being passed between them correctly, but your browser can see that it's being redirected to the same place all the time.

I can't be sure of the exact details without seeing all of the relevant code but it looks like some code is doing the equivalent of:

  1. Is the user logged in or on the login page at /pages/login/login ?
  2. Nope - redirect them to the login page at /pages/login/login

Because Nginx is proxying the request to:

http://172.16.75.99/OpenClinica/;

It always adds OpenClinica to the start of the URL so the Java application thinks it's never /pages/login/login, so it keeps redirecting.

You can fix this either by making the Java redirect be smarter or by changing the proxy setup to remove the OpenClinica path i.e.

proxy_pass http://172.16.75.99

Without a trailing slash to make the URL path be passed through to the Tomcat server exactly as the user requested it.

(You may also be able to set this by setting the appBase path for the application in Tomcat, so that it knows to expect URL paths starting with OpenClinica - but I'm not a Tomcat expert.)

Danack
  • 24,939
  • 16
  • 90
  • 122
  • thanks for your comment, my rewrite log is empty when i switch on debug to notice level, and enable the rewrite log. I added some more information to my initial post. – Patrick Werner May 05 '13 at 02:38
  • "my rewrite log is empty" That's good - it shows that Nginx isn't doing the rewriting. Which isn't too surprising as there's nothing that is doing re-writing in the config you posted. – Danack May 05 '13 at 13:10
  • Thx for the explanation. Now I'm trying to configure Tomcat with multiple vhosts. (which is really awful and quite bad documented) – Patrick Werner May 05 '13 at 21:15