Nginx configuration for multiple domains and same application

Leandro Paiva asked:

I have the following scenario:

A single application that will serve several different domains, on a single server, and a single context of a single tomcat instance. (The dns of all domains are configured and functional)

That is, I need it when the browser points to the address:

domain1.com >> redirect to >> http://127.0.0.1/websiteapp

domain2.com >> redirect to >> http://127.0.0.1/websiteapp

domainn.com.br >> redirect to >> http://127.0.0.1/websiteapp

In the application it is retrieved (via request URL) the calling domain, that is, it is my client identifier and through the domain I get the information in the database and return the configured views to that client.

I configured the NGINX (minimum configuration) like this:

server {
      listen          80;
      server_name     domain1.com.br;
      root /opt/tomcat2/webapps/websites;

      location / {
            proxy_pass http://127.0.0.1:8080/websitesapp;
            proxy_set_header  Host $http_host;
            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;


      }
}


server {
      listen          80;
      server_name     domain2.com.br;
      root /opt/tomcat2/webapps/websites;

      location / {
            proxy_pass http://127.0.0.1:8080/websitesapp;
            proxy_set_header  Host $http_host;
            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;


      }
}


server {
      listen          80;
      server_name     domainn.com.br;
      root /opt/tomcat2/webapps/websites;

      location / {
            proxy_pass http://127.0.0.1:8080/websitesapp;
            proxy_set_header  Host $http_host;
            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;


      }
}

Tomcat server.xml

<Context docBase="websitesapp" path="/" reloadable="true" source="org.eclipse.jst.jee.server:websitesapp"/>

In the application I retrieve the domain, and “mount” the a view with the information loaded from the database.

Is this setting correct?

Is there any way to pass the domain to tomcat more securely, ie without retrieving it by the browser’s URL?

Is there any contraindication to this type of approach?

Any other approach that would be more interesting?

My answer:


Except for the server_name, these server blocks are otherwise identical. You can instead use a single server block with a server_name that lists all three domains. Or twenty, if you expand later…

The $http_host is OK to use, even though the browser provided it, because it has been validated. If it did not match one of the names in server_name in one of the server blocks, it would not have been used. (Provided you have a default server, which the default configuration has).


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.