Nginx – Loadbalancing with nginx and tomcat

load balancingnginxtomcat

Hello this should be fairly easy to answer for any system admin, the problem is that I'm not server admin but I have to complete this task, I'm very close but still not managing to do it. Here is what I mean, I have two tomcat instance running on machine1 and machine2. People usually access those by visiting urls :

http://machine1:8080/appName
http://machine2:9090/appName

The problem is when I setup nginx with domain name i.e domain.com, nginx sends requests to http://machine1:8080/ and http://machine2:9090/ instead of http://machine1:8080/ and http://machine2:9090/appName

Here is my configuration (very basic as it can be noted) :

upstream backend {
  server machine1:8080;
  server machine2:9090;
}

server {

  listen 80;

  server_name www.mydomain.com mydomain.com;

  location / {

    # needed to forward user's IP address to rails
    proxy_set_header  X-Real-IP  $remote_addr;

    # needed for HTTPS
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_max_temp_file_size 0;

    proxy_pass http://backend;

  } #end location

} #end server

What changes must I do to do the following :
– when user visits mydomain.com
– transfer him to either machine1:8080/appName or machine2:9090

Thank you

Question Update:

I think this is complicated for the first timer like me. I'm going to use just one instance server instead of two, here is my configuration :

server {

  listen 80;

  server_name www.mydomain.com mydomain.com;
  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080;
    }

}

The problem :

When I visit mydomain.com I get that well known tomcat page If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!.

If I manually go to mydomain.com/myApp then everything works as expected.

Question :

Is there a way for me to configure this to do the following :

When I type in the address bar mydomain.com that I get transfered to mydomain.com/myApp

Or I'm completely off in this case. There is easier way to do this?

Update II :

When I change configuration to :

proxy_pass http://localhost:8080/appName/;

The website looks like it's working, but it really doesn't. Links don't work, css/js doesn't load.

Best Answer

It's unclear to me based on your question which machine, or both, has "appname" in the destination url. Asuming your destinations are:

http://machine1:8080/appName
http://machine2:9090/appName

I'm just taking a shot here, but I would try adding "/appName" to either the upstream block OR the proxy pass line:

upstream backend {
  server machine1:8080/appName;
  server machine2:9090/appName;
}

OR

location / {
    ...
    proxy_pass http://backend/appName;
} #end location

Documentation for the proxy_pass directive can be found here.