Configuring Primefaces application with glassfish and nginx

One of the issues I had these days was installing an Primefaces application in Glassfish application server  running in a container with no external IP address and configuring a nginx front-end for it. While making the two servers work together is just a matter of right configuration files, issues start to pile up when we want to have the context path removed from the nginx. Tried couple of approaches, using URL rewrite, proxy pass, etc, but in the end all of them ended up with links and resource files (ex. CSS files for the themes) not being found. Eventually after many tries ended up with this solution, which (so far) seems to work without any issue.

First, we deploy the application in glassfish, either from the admin interface or by copying the file in the right location.

Next step, we need to have a virtual host configuration for glassfish, listening for the host name of our choice (where the application will be accessible). For this we need to create a virtual server. We can do this the easiest using the asadmin (for me the admin interface kept throwing exception, so I had no other option):

asadmin create-virtual-server --hosts <your host name> <your server id>

 Once this is created successfully, we can move to the admin interface to continue the configuration from there. First, we need to assign a http listener to this virtual server, otherwise no requests will be received by the server. For this in the left side we open the Configurations node, then server-config node and then the Virtual Servers node. There we should have a node with our newly created virtual server. By clicking on it we can edit it’s properties and in network listeners we can assign a network listener (existing one or we can create a new one. I opted for an existing one – http-listener-1 – which seems to work without issues).

Next, we need to open the Applications node and there click onto our deployed application to edit it’s properties. In the Virtual Servers field we set our newly configured virtual server and for context path we can have / or leave it empty – does not make any difference.

We can restart the domain, just to maker sure everything falls in place, but at this point glassfish should be configured. Next, we need to configure nginx virtual host. we need to create a file with a content like this:

server {
  listen          80;
  server_name     <your server>;


  location / {

        proxy_set_header X-Forwarded-Host  <your server>:$server_port;
        proxy_set_header X-Forwarded-Server <your server>;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host <your server>;
        proxy_pass http://<your glassfish IP/address>:<your http listener port>;
  }
}

After creating the link in sites-enabled and restarting nginx, the Primefaces application should be available at specified server address.