Today's project development is basically in the form of microservices, resulting in many services in a system, and each module corresponds to different ports. In order to facilitate access, a service is usually bound with a domain name, such as commodity service: product xxx. com; Order service: order xxx. Com. At this time, you can use Nginx to build a domain name access environment. Projects based on the separation of front and rear end often encounter cross domain problems, which can be easily solved by using Nginx.
Installing Nginx
First pull the image of nginx:
docker pull nginx:1.10
Then start an nginx instance at will:
docker run -p 80:80 --name nginx -d nginx:1.10
The purpose of starting this nginx instance is to copy the configuration files in nginx:
docker container cp nginx:/etc/nginx .
In this way, an nginx folder will be generated in the current directory. Rename it to conf, then create an nginx folder, and move the conf folder into it:
mv nginx conf mkdir nginx mv conf/ nginx/
Then officially start 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.10
Make a one-to-one mapping between the nginx folder just prepared and the folder in the nginx container.
Preparing 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>
Register it with Nacos:
spring: cloud: nacos: discovery: server-addr: 192.168.66.10:8848 application: name: SpringBootDemo
Start project, access http://localhost:8080/ :

The current requirement is to access the domain name myspringboot Com can also access this page, so you can modify the hosts file in Windows:
192.168.66.10 myspringboot.com
The purpose of this paragraph is to access myspringboot COM, the actual access is 192.168.66.10, that is, our Linux system.
At this point, go to Linux and configure Nginx. The configuration files created in the conf.d directory will be automatically scanned by Nginx:
cd /mydata/nginx/conf/conf.d touch mysb.conf
Add configuration:
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; } }
This configuration means listening to myspringboot The request from COM: 80 will be processed by the location / if it is accessed / and forwarded to http://192.168.0.105:8080/:

Add gateway
Generally, Nginx will be used together with the gateway, because microservices are generally deployed in clusters. At this time, the request cannot accurately determine which service to turn to, but should be automatically loaded on each service. Therefore, a gateway should be added to realize 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>
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: 9000
At this time, modify the configuration of Nginx. First, add the configuration of the gateway in the http block:
upstream my_gateway{ server 192.168.0.105:9000 # Configure the address of the gateway }
Then modify the server block:
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; } }
Visit now http://myspringboot.com/ , the request will be sent to Nginx, and Nginx will send it to the gateway for processing. Let's configure the gateway to forward the request to the specified service for processing:
spring: cloud: gateway: routes: - id: springbootdemo_route uri: lb://SpringBootDemo predicates: - Path=/**
This configuration will listen to all requests because the value of Path is / * *. When the request comes to the gateway, it will be directly transferred to the MySpringBoot service. lb: / / means load balancing. The effect is as follows:

The current request is the specific service that finally arrives through Nginx and then through the gateway.