4

Here are the preconditions:

  • I've registered domain example.com in godaddy.
  • I run a Tomcat on 8080 port with a couple of applications. 8080 port is hidden to the world.
  • I want to map subdomain.example.com to server:8080/subdomain. I want it be possible to do similar mappings (subdomain2.example.com to server:8080/anotherContext, for example) in the future.
  • example.com should be mapped to server:8080/mainPageApp
  • All other applications should be accesible via their context: example.com/app -> server:8080/app.

For now, I've only configured godaddy (I do not know if this is good):

Godaddy domain config

Can you help me with nginx config for this case? Here is mine, but it sends infinite redirects:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html;
    }
}
server {
    listen       80;
    server_name  subdomain.localhost;

    location / {
        proxy_pass http://127.0.0.1:8080/subdomain;
    }
}
4

1 回答 1

2

Ok. I got this work. Not sure the solution is correct but:

First I need to setup a subdomain in Tomcat. Here is the answer I was inspired with.

<Host name="subdomain.example.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="subdomain" />
</Host>

Then, configure nginx with:

server {
    listen       80;
    server_name  example.com;

    location / {
        root   /path/to/domain/root;
        index  index.html;
    }
}

server {
    listen       80;
    server_name  subdomain.example.com;

    location / {
        proxy_pass        http://subdomain.example.com:8080;
        proxy_set_header  Host             $http_host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

Domain config remains as in the question - it is good.

And finally I just put subdomain.war to the webapp directory of my Tomcat and it works like a charm!

于 2013-03-23T00:49:13.297 回答