Configure Nginx Proxy Server for Tomcat 9 on Ubuntu 16.04 | 17.10 | 18.04

If you want to enable Nginx reverse proxy for Tomcat 9 webserver, the steps below should be a great place to start… We also showed you how to enable Apache2 reverse proxy for Tomcat… Enjoy¬† them both!!

Few days ago we showed students and new users how to install Apache Tomcat 9 web server on Ubuntu 16.04 / 17.10 and 18.04… Tomcat by default communicates over port 8080… which is not a standard port… You must always use the server name and port 8080 to be able to access Tomcat’s content… In some cases, this might not be what users want…

This post will show you how to setup Nginx HTTP server as a reverse proxy for Tomcat 9 webserver. A reverse proxy is a type of service where the proxy server (frontend server) takes HTTP(S) requests and forwards them to a backend server.

In this setup, Nginx will sit in front of Tomcat 9 and accepts all requests over port 80 (the default). Then those requests are transparently sent to Tomcat 9 webserver to fetch and retrieve the content….

To get started with setting up Nginx as a reverse proxy, follow the steps below

Step 1: Install Tomcat 9 Server

To use Apache Tomcat 9 as a backend server, you must install and configure it… We’ve covered that on this website… To learn how to install Tomcat webserer, read the post below:

Setup Apache Tomcat9 on Ubuntu 16.04 / 17.10 / 18.04

Step 2: Install and Configure Nginx

Now that Tomcat is installed and configured, go and install Nginx HTTP server. To do that run the commands below

sudo apt-get install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to automatically start up everytime the server starts.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Step 3: Enable Nginx Proxy Site

Unlike Apache2, Nginx by default has its proxy module enabled… so no neeed to turn on its proxy component… Simply configure the default site for the domain you wish to use and the configuration directory….

To create a new proxy site for a domain (example.com), simply run the commands below to create a new site configuration file called example

sudo nano /etc/nginx/sites-available/example

Then copy and paste the lines below into the file and save.

    server {
    listen 80;
    listen [::]:80;
    server_name  example.com www.example.com;

    proxy_redirect           off;
    proxy_set_header         X-Real-IP $remote_addr;
    proxy_set_header         X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header         Host $http_host;

    location / {
            proxy_pass http://127.0.0.1:8080;
        }
}

After that, save the file.

Nginx, run the commands below to enable the proxy site you just created above…

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/

After that, run the commands below to restart Nginx…

sudo systemctl restart nginx.service

If Tomcat fails to start, you may want to disable the default site configuration for Nginx site configuration file or change the port in the file to 8080.

sudo rm -rf /etc/nginx/sites-available/default

Now, all you have to do is type the domain name and you should be sent to Tomcat web server without typing the port number (8080)…

Tomcat proxy server ubuntu

Enjoy!

You may also like the post below:

Resetting MySQL Root Password on Ubuntu 16.04 / 17.10 and 18.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.