0
0
NginxHow-ToBeginner · 4 min read

How to Use Nginx with Spring Boot: Simple Reverse Proxy Setup

To use nginx with a Spring Boot application, configure nginx as a reverse proxy that forwards client requests to the Spring Boot server running on a specific port. This setup improves security and allows nginx to handle static content, SSL, and load balancing.
📐

Syntax

The basic nginx configuration for proxying requests to a Spring Boot app includes these parts:

  • server: Defines the server block listening on a port (usually 80 or 443).
  • location /: Matches all requests and forwards them.
  • proxy_pass: The URL where Spring Boot listens (e.g., http://localhost:8080).
  • proxy_set_header: Passes original headers like Host and X-Forwarded-For for correct client info.
nginx
server {
    listen 80;
    server_name yourdomain.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;
    }
}
💻

Example

This example shows a full nginx server block that proxies requests to a Spring Boot app running on port 8080. It listens on port 80 and forwards all traffic.

nginx
server {
    listen 80;
    server_name 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;
    }
}
Output
When you visit http://example.com, nginx forwards the request to Spring Boot on localhost:8080 and returns the response to the client.
⚠️

Common Pitfalls

Common mistakes when using nginx with Spring Boot include:

  • Not setting proxy_set_header Host, which can cause Spring Boot to generate incorrect URLs.
  • Forgetting to allow traffic on the Spring Boot port in firewall settings.
  • Not configuring Spring Boot to recognize forwarded headers, which can affect security and redirects.
  • Using proxy_pass without a trailing slash incorrectly, causing URL path issues.
nginx
Wrong proxy_pass usage:
location /api {
    proxy_pass http://localhost:8080/api;
}

Right proxy_pass usage:
location /api {
    proxy_pass http://localhost:8080/;
}
📊

Quick Reference

Tips for using nginx with Spring Boot:

  • Run Spring Boot on a non-privileged port like 8080.
  • Use nginx to handle SSL termination and static files.
  • Configure Spring Boot with server.use-forward-headers=true to handle proxy headers.
  • Test your setup by curling localhost and your domain to verify proxying.

Key Takeaways

Use nginx as a reverse proxy to forward requests to Spring Boot running on a separate port.
Always set proxy headers like Host and X-Forwarded-For for correct client info.
Configure Spring Boot to recognize forwarded headers for proper URL and security handling.
Avoid proxy_pass path mistakes by correctly using trailing slashes.
Use nginx to handle SSL and static content to improve performance and security.