Setup reverse proxy with NGINX

Start by installing the NGINX HTTP server using

apt-get install nginx

Once NGINX is installed, create the following configuration file in /etc/nginx/sites-available/glassfish.conf:

vim /etc/nginx/sites-available/glassfish.conf
upstream glassfish { 
  server weight=100 max_fails=5 fail_timeout=5;

server {
  listen          80;
  server_name     domain.tld;
  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://glassfish/appname/;

Next, navigate to /etc/nginx/sites-enabled and create a symbolic link to the glassfish.conf configuration file you just created:

cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/glassfish.conf

Now, check if Nginx configuration syntax is OK by running:

nginx -t

and restart and enable NGINX on your virtual server startup using:

service nginx restart
update-rc.d -f nginx enable

NGINX multiple server blocks

Create separately two files (you don’t have to, but it will be much clearer) in /etc/nginx/sites-available/ and /etc/nginx/sites-available/

As an example to redirect the second one to the first one

server {
  listen        80;
  return  301;