Nginx – Running multiple website domains on Wildfly

javajbossnginx

Suppose I have many websites with different domain names, domain1.com, domain2.com, etc. I have setup the correct virtualhosts configuration for these sites in wildfly. The correct DNS entries are also in place. How do I route requests to my server for these domains to wildfly?

  1. Do I simply run wildfly on ports 80 and 443? Something tells me this is a bad idea as wildfly does not run on these ports by default. To run wildfly on those ports require binding to those ports as a privileged user and then switching to an unprivileged user. The hassle and difficulty of doing this suggests that this isn't right.

  2. Do I run wildfly behind nginx or apache2? My app spins up websites and virtualhost config dynamically. Can I just forward all requests to wildfly and let it deal with it?

  3. ???

Best Answer

Option 1 is possible, but as you correctly predicted it is not a preferred approach. In order to run on port 80 or 443, you need a privileged user and you are directly exposing the java server to the public, making it easier to get into trouble.
So fire up nginx, define the domains there and configure upstream and proxy_pass for reverse proxy to your wildfly instances. You get loadbalancing for free, you can handle ssl on nginx instead of wildfly and the whole setup is just so much easier to maintain and scale. You can either setup dedicated wildfly cluster for each domain and have a separate upstream definition, or you can start with a single instance with separate webcontexts e.g.

upstream jboss {
    server 192.168.33.11:8080;
}
server {
    listen 443;
    server_name example.com;

    location / {
         proxy_pass http://jboss/webapp1;
    }
} 
server {
    listen 443;
    server_name otherdomain.com;
    #pass the request to another .war dewployed on jboss
    location / {
         proxy_pass http://jboss/webapp2;
    }
} 

You can find more info on nginx website

Related Topic