jar package process resolution for Nginx reverse proxy springboot

  • 2020-08-22 23:27:33
  • OfStack

The common way to deploy springboot projects to a server is to package war and deploy Tomcat or package jar and use the built-in package to run easily. Many people now package war and deploy it to tomcat, which is fine but difficult to maintain later. From the official statement, jar deployment is the best way, but there is a problem if you deploy multiple ES8en-ES9en project ports at the same time, how to access them through the domain name, then you need Nginx to deliver. Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. Good for deploying springboot, the installation of Nginx is not shown here as a tutorial showing the configuration of the main ES18en.conf

Sample 1;


server {
  listen    80;
  server_name 127.0.0.1;
  access_log logs/book.log;
  error_log logs/book.error;
 
  # will /wx-service Request forward to http://127.0.0.1:8011/wx-service To deal with 
  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;
  }
 
  # will /bootdo Request forward to http://127.0.0.1:8012/bootdo To deal with 
  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;
  }
 
  # will /xcloud-service Request forward to http://127.0.0.1:8013/xcloud-api To deal with 
  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;
  }
 
  # will /eureka-service Request forward to http://127.0.0.1:8081/eureka-service To deal with 
  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;
  }
 
  # will /xcloud-api Request forward to http://127.0.0.1:8082/xcloud-api To deal with 
  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;
    }
    
    # will /wvv Request forward to http://127.0.0.1:1992/wvv To deal with 
    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 the path to a different one

# will forward/wvv request to http: / / 127.0.0.1:1991 / project 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;
  }

If you change the port of nginx to 80 port domain, you can directly access service_name for localhost, which stands for native localtion /XXX. In this case, you can configure nginx port to forward several items. You can also configure tomcat items in sbin

Provide 1 spring-ES53en quick restart shell script in this pro test 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 named xx.sh

It is recommended that you create a separate folder under nginx with the project name, put the jar package into it, and then launch the jar package.

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

Remember that the springboot project has to be configured in the configuration file

[

server:
context-path: /xcloud-api

]

Spring boot by default is/so you can access the index page directly via http://ip:port/ but to configure multiple projects via nginx you have to specify context-ES90en for each project

Create a new folder in the server directory to hold spring-ES94en packaged and restart scripts like this

This makes it easy to manage the logs directory is the log folder generated after starting the script ignore 1 project for 1 folder containing project jar and 1 restart shell script

This allows you to start multiple springboot projects in the background and access them through one domain name. If you want to view the logs in real time, go to the logs-directory for each project file

If there is any deficiency, please kindly point out...


Related articles: