Step 5: Configure Reverse Proxy with Nginx

Deploy Spring Boot Apps to AWS Lightsail

2026-04-08

Overview

What We’ll Cover:

  • Understanding reverse proxy architecture
  • Installing and configuring Nginx
  • Advanced configuration options
  • Testing and monitoring

Learning Objectives:

  • Set up Nginx as reverse proxy
  • Implement security headers
  • Configure rate limiting
  • Optimize performance

Why Use a Reverse Proxy?

Performance Benefits: - Serve static content directly - Cache responses - Gzip compression - Load balancing capability

Security Benefits: - Hide application server details - SSL termination - Request filtering - Security headers

Reverse Proxy Architecture

graph LR
    A[Client] --> B[Nginx :80]
    B --> C[Spring Boot :8080]
    B --> D[Static Files]
    B --> E[Cache]

Flow: Client → Nginx (Port 80) → Spring Boot (Port 8080)

Install Nginx

# Update package lists
sudo apt update

# Install Nginx
sudo apt install nginx -y

# Check if Nginx is running
sudo systemctl status nginx

# Enable Nginx to start on boot
sudo systemctl enable nginx

Configure Firewall

# Allow HTTP traffic (port 80)
sudo ufw allow 'Nginx HTTP'

# Allow HTTPS traffic (port 443)
sudo ufw allow 'Nginx HTTPS'

# Check firewall status
sudo ufw status

Don’t forget: Also open ports 80 and 443 in Lightsail’s Networking tab!

Create Nginx Configuration

# Create configuration file
sudo nano /etc/nginx/sites-available/spring-boot-app

Key Configuration Sections: - Server block and domain settings - Security headers - Gzip compression - Proxy settings

Basic Server Configuration

server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    
    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    
    # Main application proxy
    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;
    }
}

Enable Gzip Compression

# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml 
           text/javascript application/javascript 
           application/xml+rss application/json;

Benefits: Reduces bandwidth usage by up to 70%

Static Content Optimization

# Static content location
location /static/ {
    alias /home/ubuntu/app/static/;
    expires 1y;
    add_header Cache-Control "public, immutable";
}

# Optimize static assets
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    access_log off;
}

Enable the Configuration

# Create symbolic link to enable the site
sudo ln -s /etc/nginx/sites-available/spring-boot-app \
           /etc/nginx/sites-enabled/

# Remove default Nginx site
sudo rm /etc/nginx/sites-enabled/default

# Test Nginx configuration
sudo nginx -t

# Reload Nginx to apply changes
sudo systemctl reload nginx

Implement Rate Limiting

# Add to /etc/nginx/nginx.conf http block
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=general:10m rate=1r/s;

# In server block
location /api/auth/login {
    limit_req zone=login burst=3 nodelay;
    proxy_pass http://localhost:8080;
}

Protection against: DDoS attacks, brute force attempts, API abuse

Custom Error Pages

# Custom error pages
error_page 502 503 504 /50x.html;
location = /50x.html {
    root /var/www/html;
}

# Health check endpoint (bypass proxy for quick responses)
location /actuator/health {
    proxy_pass http://localhost:8080;
    proxy_connect_timeout 2s;
    proxy_read_timeout 2s;
}

Testing Your Configuration

Server Tests:

# Check Nginx status
sudo systemctl status nginx

# Verify port listening
sudo netstat -tlnp | grep :80

# Test locally
curl -I http://localhost

Browser Tests: - Navigate to public IP - Check developer tools for headers - Verify application loads correctly - Test static content serving

Troubleshooting Common Issues

  • 502 Bad Gateway → Spring Boot app not running on port 8080
  • Connection refused → Firewall blocking ports 80/443
  • Configuration errors → Run sudo nginx -t to validate
  • Performance issues → Check logs and system resources

Log Locations: - /var/log/nginx/access.log - /var/log/nginx/error.log

Monitoring and Maintenance

# Set up log rotation
sudo nano /etc/logrotate.d/nginx-spring-boot

# Monitor performance
ps aux | grep nginx
curl http://localhost/nginx_status
htop

Best Practices: - Regular log rotation - Monitor system resources - Track response times - Set up alerting

Security Headers Explained

X-Frame-Options: "SAMEORIGIN"
X-Content-Type-Options: "nosniff"
X-XSS-Protection: "1; mode=block"
Referrer-Policy: "no-referrer-when-downgrade"

Protection Against: - Clickjacking attacks - MIME type sniffing - Cross-site scripting - Information leakage

Performance Optimization Tips

{.incremental} - Enable gzip compression for text content - Set appropriate cache headers for static assets - Use separate location blocks for different content types - Implement connection keep-alive for better performance - Configure proper timeout values - Monitor and tune worker processes

Summary

What We Accomplished: - ✅ Installed and configured Nginx as reverse proxy - ✅ Implemented security headers and rate limiting - ✅ Optimized performance with compression and caching - ✅ Set up proper error handling and monitoring

Key Benefits: - Enhanced security and performance - Professional error handling - Production-ready architecture - Better monitoring and maintenance

Next Steps

Coming Up in Step 6: - Add custom domain configuration - Implement SSL/TLS certificates - Configure HTTPS redirects - Final security hardening

Your application is now: - Accessible through Nginx on port 80 - Protected with security headers - Optimized for performance - Ready for domain and SSL setup