Spring Boot Deployment with NGINX Proxy

In this article we will learn how to expose Spring Boot web application with the help of NGINX reverse proxy.

Install NGINX

Let’s begin with installation of NGINX, for this tutorial we will be using Ubuntu linux. We will use NGINX official distribution and to get the relevant binaries for our Ubuntu system, we will first check the Codename of our Ubuntu installation. Run following command to get the Codename of Ubuntu Instalation.

 lsb_release -a

The output of this command will be something like following.

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.6 LTS
Release:	16.04
Codename:	xenial

In this case the code name is xenial.

Now we will add the link to NGINX official repository to sources.list of ubuntu. Open /etc/apt/sources.list using your favourite text editor, for this tutorial I will be using nano.

sudo nano /etc/apt/sources.list

Add following line to the end of sources.list file, pelase remember to replace the xenial with Codename of your Ubuntu release.

deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx

Save and exit the editor, for nano press Ctrl + O to save the file and Ctrl + X to exit the editor.

Run following command to download signing key for NGINX repository.

sudo wget http://nginx.org/keys/nginx_signing.key

Add the repository signing key (downloaded in previous step) to apt.

sudo apt-key add nginx_signing.key

If the command was successful it will print OK on screen.

OK

Now update the apt package manager index using the apt update command.

sudo apt update

Finally we will install the NGINX using apt package manager as following.

sudo apt install nginx

Run following command to check if NGINX was successfully installed.

nginx -v

Above command will print the version string of installed NGINX server.

nginx version: nginx/1.19.2

Run following command to enable NGINX service.

sudo systemctl enable nginx

Output of this command will be something like.

Synchronizing state of nginx.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable nginx
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").

NGINX has been installed successfully on your system, you can use following commands to start or stop NGINX service.

sudo service nginx start
sudo service nginx stop

Run following command to check the current status of NGINX servcie.

sudo service nginx status

This will print current status of NGINX service as following.

● nginx.service - nginx - high performance web server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-09-24 19:49:40 UTC; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 6753 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 6755 (nginx)
    Tasks: 2
   Memory: 1.3M
      CPU: 7ms
   CGroup: /system.slice/nginx.service
           ├─6755 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.con
           └─6756 nginx: worker process                   

Sep 24 19:49:40 ams-dev systemd[1]: Starting nginx - high performance web server...
Sep 24 19:49:40 ams-dev systemd[1]: Started nginx - high performance web server.

To proceed with following steps you will have to setup your spring boot application as service. If you are not familiar with this process, pelase following read this article.

NGINX Configuration

In context of this article we will assume Spring Boot application is up and running on HTTP port 8080.

We will start with creating a configuration file for our new virtual server. Remember to replace the name of the conf file "testserver" accordingly.

sudo nano /etc/nginx/conf.d/testserver.conf

Copy following configuration to testserver.conf.

server {
        listen 80;
        listen [::]:80;

        server_name testserver.com;

        location / {
             proxy_pass http://localhost:8080/;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header X-Forwarded-Port $server_port;
        }
}

Shoket Mahmood Ahmed

Leave a Reply

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

Back to top