Setup Nginx and SSL for Spring Boot Application

Setup Nginx and SSL for Spring Boot Application

Setting Nginx as a reverse proxy in our Spring Boot application

In this tutorial, we are going to set up Nginx for our Spring Boot application.

Prerequisites

Install and Configure Nginx

First, install Nginx for reverse proxy setup
bash
sudo apt update
sudo apt install nginx
If you open your public IP address or domain URL with port 8080, you can see the nginx landing page.

Create the Reverse Proxy Config

Create a file for Nginx Configuration:
bash
sudo vi /etc/nginx/sites-available/myapp
Give your domain name instead of myapp. Add the following configuration in your file:
bash
server {
    listen 80;
    server_name api.example.com www.api.example.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Here, proxy_pass will forward every request from your domain api.example.com to your running Spring Boot application.

Enable the Configuration

Use the following command to enable our configuration file:
bash
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Check the syntax error

bash
sudo nginx -t
This must result in the successful output:
bash
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reload Nginx

bash
sudo systemctl restart nginx
You can see the status of your running nginx as:
bash
sudo systemctl restart nginx
The output will look like this:
bash
 nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Sun 2026-05-03 09:56:51 UTC; 21h ago
       Docs: man:nginx(8)
   

Set Up SSL with Let's Encrypt

Now lets setup HTTPs for our Spring Boot application. We are using certbot to manage this.

Install Certbot

sudo apt install python3-certbot-nginx

Obtain and Install Certificate

bash
sudo certbot --nginx -d yourdomain.com
This will create the SSL certificate and update your nginx config file. The sample updated config file looks like this: You can verify with sudo vi /etc/nginx/sites-available/myapp
bash
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = your-domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name your-domain.com;
    return 404; # managed by Certbot


}
Now reload the Nginx.

Handle CORS in Spring Boot

You might get a CORS issue if you have a different domain that wants to communicate with your Spring Boot app. We need to set up and handle the CORS. Add the following config to your Spring application.
bash
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("https://your-frontend-doamin.com", "https://www.your-frontend-doamin.com")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
}
We have configured Nginx and installed SSL for HTTPS.