This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Nowadays, project development is basically based on microservices, resulting in a system with many services, and each module corresponds to a different port. In order to facilitate access, a service is usually bound to a domain name, such as product.xxx.com. Order service: order.xxx.com. At this time, Nginx can be used to build a domain name access environment. Projects based on the separation of front and back end often encounter cross-domain problems, which can be easily solved with Nginx.

Install Nginx

First pull the nginx image:

Docker pull nginx: 1.10Copy the code

Then randomly start an instance of Nginx:

Docker run -p 80:80 --name nginx -d nginx:1.10Copy the code

The purpose of starting the nginx instance is to copy the configuration files from nginx:

docker container cp nginx:/etc/nginx .
Copy the code

Rename the nginx folder to conf, then create an nginx folder and move the conf folder into it:

mv nginx conf
mkdir nginx
mv conf/ nginx/
Copy the code

Then launch a new nginx instance:

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.10Copy the code

Map the nginx folder you just prepared to the folders in the Nginx container.

Prepare the SpringBoot application

Create a SpringBoot application and introduce dependencies:

<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>
Copy the code

To register it with Nacos:

Spring: Cloud: nacos: Discovery: server-addr: 192.168.66.10:8848 Application: Name: SpringBootDemoCopy the code

Start the project, accesshttp://localhost:8080/ : Now the need is to access the domain namemyspringboot.comYou can also access the page, so modify the hosts file in Windows:

192.168.66.10 myspringboot.com
Copy the code

The effect of this is that when you access myspringBoot.com, you are actually accessing 192.168.66.10, which is our Linux system.

Nginx will automatically scan the configuration files created in the conf.d directory.

cd /mydata/nginx/conf/conf.d
touch mysb.conf
Copy the code

Add configuration:

server { listen 80; server_name myspringboot.com; The 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; }}Copy the code

This configuration listens for requests from mySpringBoot.com :80, if accessed/The request will be processed by location /, forwarding the request tohttp://192.168.0.105:8080/:

Add the gateway

In general, Nginx is used in conjunction with a gateway, because microservices are clustered, and requests cannot determine exactly which service to redirect to. Instead, they are automatically loaded onto each service, so a gateway should be included to implement this function.

Create a SpringBoot application and introduce dependencies:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code

You also need to register the gateway with Nacos:

Spring: Cloud: nacos: Discovery: server-addr: 192.168.66.10:8848 Application: Name: MyGateway Server: port: 9000Copy the code

To change the Nginx configuration, first add the gateway configuration to the HTTP block:

Upstream my_gateway{server 192.168.0.105:9000}Copy the code

Then modify the server block:

server { listen 80; server_name myspringboot.com; location / { proxy_pass http://my_gateway; } error_page 500 502 503 504/50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code

When you visit myspringboot.com/, the request will be forwarded to Nginx, which will then be forwarded to the gateway. Let’s configure the gateway to forward the request to the specified service:

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

This configuration listens for all requests because the value of Path is/ * *When the request comes to the gateway, it is forwarded directly to the MySpringBoot service,lb://Indicates load balancing. The effect is as follows:The request is now the specific service that goes through Nginx and then through the gateway.