Nginx reverse proxy springboot jar package process analysis

Nginx reverse proxy springboot jar package process analysis

The common way to deploy a springboot project to a server is to package it into a war package and deploy it in Tomcat, or package it into a jar package and use the built-in easy-to-run package directly. Many people now package it into a war package and deploy it to tomcat. Although this method is fine, the subsequent maintenance is more troublesome. According to the official instructions, jar deployment is the best way, but this raises another problem: if multiple spring-boot projects are deployed at the same time and the ports are different, how can they be accessed through the domain name? Next, Nginx is needed. Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. It is very suitable for deploying springboot. The installation of Nginx is not a tutorial here. The main configuration of nginx.conf is given

Sample 1;

server {
  listen 80;
  server_name 127.0.0.1;
  access_log logs/book.log;
  error_log logs/book.error;
 
  #Forward the /wx-service request to http://127.0.0.1:8011/wx-service for processing location /wx-service {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8011/wx-service;
  }
 
  #Forward the /bootdo request to http://127.0.0.1:8012/bootdo for processing location /bootdo {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8012/bootdo;
  }
 
  #Forward the /xcloud-service request to http://127.0.0.1:8013/xcloud-api for processing location /xcloud-service {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8013/xcloud-api;
  }
 
  #Forward the /eureka-service request to http://127.0.0.1:8081/eureka-service for processing location /eureka-service {
    proxy_pass http://127.0.0.1:8081/eureka-service; #Remember to change the port here to the corresponding project proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
  }
 
  #Forward /xcloud-api request to http://127.0.0.1:8082/xcloud-api for processing location /xcloud-api {
    proxy_pass http://127.0.0.1:8082/xcloud-api; #Remember to change the port here to the corresponding project proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
  }
}

Example 2:

 server {
    listen 80;
    server_name localhost;
    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
      root html;
      index index.html index.htm index.php;
    }
    
    #Forward the /wvv request to http://127.0.0.1:1992/wvv for processing location /wvv {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:1992/wvv;
  }
 }

If you are deploying multiple SpringBoot projects, you can add the following configuration multiple times, as long as you change it to a different path

#Forward the /wvv request to http://127.0.0.1:1991/project for processing

    location /project{
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:1991/project;
  }

Change the port of nginx to port 80, and you can access it directly. The domain name can be localhost, which represents the local localtion/XXX. This is to configure nginx port forwarding. You can configure as many projects as you want. You can also configure the projects in tomcat here. According to the above configuration, execute ./nginx -s reload in the sbin directory to refresh the nginx configuration and it will take effect.

Here is a spring-boot quick restart shell script that is tested and effective

export JAVA_HOME=/usr/local/java/jdk1.8.0_162
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 
Port=8081
JarName=clouddo-server.jar
LogsPatch=./logs_$Port
 
 
ID=`ps -ef | grep $Port | grep -v "grep" | awk '{print $2}'` 
echo $ID 
echo "--------------" 
for id in $ID 
do 
kill -s 9 $id 
echo "killed $id" 
done 
echo "--------------" 
 
rm -rf $LogsPatch
mkdir $LogsPatch
 
export LANG=zh_CN.UTF-8
 
set -m 
 
nohup java -jar -Dlogging.path=$LogsPatch $JarName>$LogsPatch/catlina.out 2>&1 &
 
tail -f $LogsPatch/catlina.out

Save and name it as xx.sh

It is recommended to create a separate folder under nginx named after the project, put the jar package into it, and then start the jar package.

java -jar revenue-1.0.jar >revenue.txt &

Remember that the springboot project must be configured in the configuration file

server:
context-path: /xcloud-api

The default setting for Spring boot is / so you can access the index page directly through http://ip:port/. However, if we want to configure multiple projects through nginx, we need to specify context-path for each project separately.

Create a new folder in the server directory according to your personal preferences to store the jar packaged by spring-boot and the restart script similar to this

This makes it easier to manage the logs directory, which is the log folder generated after the startup script is ignored. One project corresponds to a folder containing the project jar and a restart shell script.

In this way, you can start multiple springboot projects in the background at the same time and access these projects through a domain name. If you want to view the logs in real time, please go to the logs-directory of each project file and execute

You can check it by tail -f catlina.out. If there is any deficiency, please point it out and forgive me...

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Spring boot implements ultra-lightweight gateway method (reverse proxy, forwarding)
  • Spring boot2X load balancing and reverse proxy implementation process analysis
  • Springboot implements VNC reverse proxy function

<<:  Workerman writes the example code of mysql connection pool

>>:  MySQL 8.0.19 Installation Tutorial

Recommend

How to implement https with nginx and openssl

If the server data is not encrypted and authentic...

Example of fork and mutex lock process in Linux multithreading

Table of contents Question: 1. First attempt 2. R...

Two solutions for Vue package upload server refresh 404 problem

1: nginx server solution, modify the .conf config...

Basic operations of mysql learning notes table

Create Table create table table name create table...

SASS Style Programming Guide for CSS

As more and more developers use SASS, we need to ...

Implement group by based on MySQL to get the latest data of each group

Preface: The group by function retrieves the firs...

Detailed explanation of execution context and call stack in JavaScript

Table of contents 1. What is the execution contex...

jQuery implements font size adjustment case

This article shares the specific code of jQuery t...

An article to help you understand Js inheritance and prototype chain

Table of contents Inheritance and prototype chain...

Centos7 installation of Nginx integrated Lua sample code

Preface The computer I use is a Mac, and the oper...

PHP related paths and modification methods in Ubuntu environment

PHP related paths in Ubuntu environment PHP path ...

MySQL 5.7.20 compressed version download and installation simple tutorial

1. Download address: http://dev.mysql.com/downloa...