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 :


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.

  • You may think to shared session, you'll got a problem with that, that's why i recommend you to use haproxy instead of nginx. – Ali Mezgani Nov 22 '12 at 22:42

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


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;


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

Documentation for the proxy_pass directive can be found here.

  • 1
    they both have /appName in the destination path. This is dummy name for my application (appName). Both run the same application – London Nov 23 '12 at 0:20
  • Well that's what I thought you meant, and that's how I structured my answer. If you have only one server, as is the case in your updated text, you would implement my second suggestion above. However, if you have two destination servers, I'm not sure if updating the location block would work. Hence my suggestion for updating the upstream block. – chue x Nov 23 '12 at 0:33
  • when I use your suggestion with proxy pass, my static resources seem not to be read at all or something. The site looks like it has no CSS – London Nov 23 '12 at 0:52
  • please take a look at my update – London Nov 23 '12 at 0:59
  • This is getting tricky, because your configuration has changed. When I look at the docs, it seems like one or the other of my suggestions should work for you original config. In your latest config, you should see what happens when you go directly to your destination machine. In other words, what do you see when you put localhost:8080 in the browser? – chue x Nov 23 '12 at 1:28

You simply want a HTTP 302 (a redirect).

rewrite ^/*$ https://www.example.com/appName/ redirect;

The regex will catch https://www.example.com along with https://www.example.com/ and https://www.example.com///. The new url will be shown to user of the browser.

I know the answer is about 5 years late, but the thread got bumped and I saw that "someone is wrong on the Internet".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.