Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reverse proxy the jar package of springboot by Nginx

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains how Nginx reverse proxy springboot jar package, the content is clear, interested friends can learn, I believe that after reading it will be helpful.

The common way to deploy a springboot project to a server is to deploy Tomcat in a war package or directly in a jar package and use the built-in easy to run. Many people now deploy to tomcat in a war package, which is no problem, but later maintenance is more troublesome. It is the best way to deploy jar from the official instructions, but there is another problem. If multiple spring-boot project ports are not the same at the same time, how to access them through the domain name? then you need Nginx. Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. It is very suitable for deploying the installation of springboot,Nginx. I am not doing a tutorial here to give you the configuration of the main nginx.conf.

Example one

Server {listen 80; server_name 127.0.0.1; access_log logs/book.log; error_log logs/book.error; # forwards / wx-service request to http://127.0.0.1:8011/wx-service 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 / bootdo request to http://127.0.0.1:8012/bootdo 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 / xcloud-service request to http://127.0.0.1:8013/xcloud-api 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 / eureka-service request to http://127.0.0.1:8081/eureka-service processing location / eureka-service {proxy_pass http://127.0.0.1:8081/eureka-service; # remember to change the port here to the project corresponding to oh 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 processing location / xcloud-api {proxy_pass http://127.0.0.1:8082/xcloud-api; # remember to change the port here to the project corresponding to oh 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 / wvv request to http://127.0.0.1:1992/wvv 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 / 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;}

If you change the port of nginx to an 80-port domain name, you can directly access service_name and represent native localtion / XXX for localhost. This is to configure nginx port forwarding. If you do not lose a few items, you can also configure several items in tomcat. According to the above configuration, you can also configure items in the sbin directory. / nginx-s reload refresh nginx configuration will take effect.

Provide a spring-boot quick restart shell script here to test it effectively

Export JAVA_HOME=/usr/local/java/jdk1.8.0_162export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar Port=8081JarName=clouddo-server.jarLogsPatch=./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 $LogsPatchmkdir $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 xx.sh

It is recommended that you set up a separate folder under nginx named after the project name, then 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 has to be configured in the configuration file

Server:

Context-path: / xcloud-api

Spring boot defaults to / so that the index page can be accessed directly through http://ip:port/, but if we want to configure multiple projects through nginx, we have to specify a separate context-path for each project.

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

This makes it easy to manage the logs directory. The log folder generated after starting the script ignores a project corresponding to a folder that contains the project jar and a restart shell script.

In this way, multiple springboot projects can be started in the background at the same time and accessed through a domain name. If you want to view logs in real time, go to the logs- directory of each project file for execution.

Tail-f catlina.out can view if there are deficiencies, please point out a lot of understanding.

After reading the above content, do you have a further understanding of how Nginx reverse proxies springboot's jar package? if you want to learn more, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report