How to install the Jetty Java server with an NGINX reverse proxy

If you build web-based applications, you might consider serving them up with the Jetty Java server. Find out how to install this and an NGINX reverse proxy together.

How to install the Jetty Java server with an NGINX reverse proxy

Jetty is an HTTP server and Java Servlet container for machine to machine communications within larger software frameworks. Jetty is free, open source, and used in projects like Apache ActiveMQ, Alfresco, Scalatra, Apache Maven, Google App Engine, Eclipse, Twitter's Streaming API, and more. 

Jetty is growing in popularity because it is easily embeddable, very light weight, and an option for both static and dynamic content. With Jetty you can run both your web server and web applications in the same process, without suffering interconnection overheads and complications. 

To demonstrate the flexibility of Jetty, I'm going to walk you through the process of installing Jetty as a Java HTTP server on Ubuntu Server 18.04, using NGINX as a reverse proxy. 

SEE: Choosing your Windows 7 exit strategy: Four options (TechRepublic Premium)

What you'll need 

The only things you'll need to make this work are:

  • A running, updated instance of Ubuntu Server 18.04

  • A user with sudo privileges

How to install Jetty

The first thing to do is the installation of Jetty. Before that can be installed, Java must first be added with the command:

sudo apt-get install default-jdk -y

When that installation completes, install Jetty with the command:

sudo apt-get install jetty9 -y

Start and enable Jetty with the commands:

sudo systemctl start jetty9
sudo systemctl enable jetty9

How to install and configure NGINX

We need to first install NGINX to serve as a reverse proxy for Jetty. Before you do this, you must stop and disable Apache (otherwise, NGINX will not install). This can be accomplished with the commands:

sudo systemctl stop apache2
sudo systemctl disable apache2

With Apache stopped and disabled, install NGINX with the command:

sudo apt-get install nginx -y

Start and enable NGINX with the commands:

sudo systemctl start nginx
sudo systemctl enable nginx

Create an NGINX virtual host configuration for Jetty with the command:

sudo nano /etc/nginx/sites-available/jetty.conf

In that file, paste the following contents:

upstream jetty {
  server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5;
}

server {
  listen          80;
  server_name     example.com;

  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_pass http://jetty/;
  }
}

Save and close the file.

Enable the new virtual host with the command:

sudo ln -s /etc/nginx/sites-available/jetty.conf /etc/nginx/sites-enabled/

Restart NGINX with the command:

sudo systemctl restart nginx

How to access Jetty

To make sure your Jetty and NGINX instances are running, open a web browser and point it to http://SERVER_IP:8080 (Where SERVER_IP is the IP address of the hosting server). You should see the Jetty welcome screen (Figure A).

Figure A

jettya.jpg

The Jetty welcome screen means it's ready to serve you.

If the NGINX welcome page comes up, you'll need to move that index file with the command:

sudo mv /var/www/html/index.nginx-debian.html ~/

Restart NGINX once again and the Jetty page should appear.

And that's all there is to installing Jetty and using NGINX as a reverse proxy for the server. You can now start building your sites and applications with this flexible Java server.

Also see

Row of network servers in data center

Getty Images/iStockphoto