Nginx – Configuring nginx as tomcat / uWSGI proxy

nginxPROXYtomcat

i'm in the process of configuring an amazon EC2 machine for java and django development. The stack we've chosen is nginx as the top level listener on :80 port, and passing the requests to the respective applications, thro :8080 for tomcat and thro a .sock for uWSGI.

I have nginx configured in the following way:

   server {
             listen       80;
             server_name  test.myproject.com;
             rewrite ^/(.*)$ /myproject/$1;
             location / {

                         proxy_pass http://127.0.0.1:8080/;
                         proxy_set_header Host $host;
                         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             }
    }
    server {
              listen       80;
              server_name  test.myotherproject.com;
             rewrite ^/(.*)$ /myotherproject/$1;
             location / {
                         proxy_pass http://127.0.0.1:8080/;
                         proxy_set_header Host $host;
                         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             }
    }
    server {
             listen       80;
             server_name  web1.myhost.com.br;

             location /static/ {
                     root /home/ubuntu/projects/hellow/;
             }

             location / {
                 uwsgi_pass      unix:/var/run/uwsgi.sock;
                 include         uwsgi_params;
                 uwsgi_param     SCRIPT_NAME '';
             }

    }

The first 'server' entry redirect to a tomcat webapp .war and its working flawlessly. the third is redirecting to the uWSGI sock and its also working fine. the second one is the problem. it is configured in the same way as the first, but when I access http://test.myotherproject.com my browser gives me a 404 with the following message:

    HTTP Status 404 - /myotherproject/myotherproject/user/redirectPermission

    type Status report

    message /myotherproject/myotherproject/user/redirectPermission

    description The requested resource is not available.

    Apache Tomcat/7.0.33

/user/redirectPermission is a valid page, and it is the expected behaviour of the application to go there on the first access, to get the user authenticated. The problem is the second /myotherproject/ part that gets appended, and I cant figure out why or where this is happening.
Note that the url that give me the 404 above is: http://test.myotherproject.com.br/myotherproject/user/redirectPermission .
Also the tomcat have no extra configurarion in the server.xml file other than changing its port to 8080, and the war is at webbapps and its named myotherproject.war (tomcat7.x/webapps/myotherproject.war).
Does anyone know how configure this setup properly?
Thanks in Advance,
Salvia

Best Answer

Can you perform a simple test?

$ curl -I http://test.myproject.com/myproject
$ curl -I http://test.myotherproject.com/myotherproject

The output for the second, if I'm right, is returning a 301 (or similar) to /myotherproject/user/redirectPermission , which is rewritten as /myotherproject/myotherproject/user/redirectPermission by the rewrite rules:

rewrite ^/(.*)$ /myotherproject/$1;

If this is indeed the problem, a solution is to make both applications to be served in the root context, but configured as virtual hosts on Tomcat, so that it decides which application to serve based on the Host header (you might need to pass this header explicitly from nginx to Tomcat).

But the proper solution is to just forward the whole request to Tomcat based on a location rule, and let Tomcat handle it the way it wants.