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

In-depth explanation of environment variables and configuration files in CentOS

Preface The CentOS environment variable configura...

Usage of HTML H title tag

The usage of H tags, especially h1, has always bee...

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting string...

Axios secondary encapsulation example Demo in the project

1. Why do packaging? Facilitates overall code cal...

Vue component library ElementUI implements table loading tree data tutorial

ElementUI implements a table tree list loading tu...

MySQL cross-table query and cross-table update

Friends who have some basic knowledge of SQL must...

Detailed steps to install VMware Tools from scratch (graphic tutorial)

VMware Tools is a tool that comes with VMware vir...

Quickly get started with VUE 3 teleport components and usage syntax

Table of contents 1. Introduction to teleport 1.1...

Understanding what Node.js is is so easy

Table of contents Official introduction to Node.j...

Nginx uses ctx to realize data sharing and context modification functions

Environment: init_worker_by_lua, set_by_lua, rewr...

Vue implements calling PC camera to take photos in real time

Vue calls the PC camera to take pictures in real ...

The past two years with user experience

<br />It has been no more than two years sin...

Vue project realizes paging effect

The paging effect is implemented in the vue proje...

MySQL stored procedure in, out and inout parameter examples and summary

Stored Procedures 1. Create a stored procedure an...