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

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

How to improve MySQL Limit query performance

In MySQL database operations, we always hope to a...

Vue implements user login switching

This article example shares the specific code of ...

Use the more, less, and cat commands in Linux to view file contents

In Linux, the commands cat, more, and less can al...

MySQL 8.0.20 installation and configuration tutorial under Docker

Docker installs MySQL version 8.0.20 for your ref...

Steps to package and deploy the Vue project to the Apache server

In the development environment, the vue project i...

Do you know the weird things in Javascript?

Our veteran predecessors have written countless c...

MySQL backup and recovery design ideas

background First, let me explain the background. ...

Tutorial on installing JDK Tomcat MySQL on Linux (remote access using Mac)

One environment Alibaba Cloud Server: CentOS 7.4 ...

Detailed explanation of Nginx timed log cutting

Preface By default, Nginx logs are written to a f...

An article to help you understand jQuery animation

Table of contents 1. Control the display and hidi...

How to use Antd's Form component in React to implement form functions

1. Construction components 1. A form must contain...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...