Cloud Native 7 min read

Deploying Nginx with Docker for SpringBoot Microservices and Gateway Integration

The guide shows how to deploy an Nginx container with Docker, configure it to proxy a custom domain to a SpringBoot microservice registered in Nacos, add a Spring Cloud Gateway upstream, and route all requests through Nginx, simplifying access and eliminating CORS issues.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Deploying Nginx with Docker for SpringBoot Microservices and Gateway Integration

In microservice projects each service often has its own port; binding a domain name and using Nginx can simplify access and solve CORS.

Install Nginx

Pull the Nginx image and start a container:

docker pull nginx:1.10
docker run -p 80:80 --name nginx -d nginx:1.10

Copy the configuration files from the container: docker container cp nginx:/etc/nginx . Rename the extracted folder to conf, create an nginx directory and move the config inside:

mv nginx conf
mkdir nginx
mv conf/ nginx/

Start a new Nginx instance with volume mappings:

docker run -p 80:80 --name nginx \
    -v /mydata/nginx/html:/usr/share/nginx/html \
    -v /mydata/nginx/logs:/var/log/nginx \
    -v /mydata/nginx/conf:/etc/nginx \
    -d nginx:1.10

Prepare a SpringBoot application

Add Maven dependencies for Thymeleaf, Web and Nacos discovery:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

Register the service in Nacos:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.66.10:8848
  application:
    name: SpringBootDemo

Run the application and verify http://localhost:8080/.

Map a custom domain by adding an entry to the Windows hosts file: 192.168.66.10 myspringboot.com On the Linux host, create a configuration file in /mydata/nginx/conf/conf.d (e.g., mysb.conf) with:

server {
    listen       80;
    server_name  myspringboot.com;

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

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Add a gateway

Register a Spring Cloud Gateway service in Nacos (dependencies include spring-cloud-starter-gateway) and configure its application name and port.

Define an upstream in Nginx:

upstream my_gateway{
        server 192.168.0.105:9000; # gateway address
}

Update the server block to forward requests to the gateway:

server {
    listen       80;
    server_name  myspringboot.com;

    location / {
        proxy_pass http://my_gateway; # forward to gateway
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Configure the gateway routes to forward all paths to the SpringBoot service:

spring:
  cloud:
    gateway:
      routes:
        - id: springbootdemo_route
          uri: lb://SpringBootDemo
          predicates:
            - Path=/**

Now accessing http://myspringboot.com/ goes through Nginx, then the gateway, and finally reaches the SpringBoot service.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerMicroservicesNacosNginxSpringBootgateway
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.