Home > Java > javaTutorial > Nginx reverse proxy springboot jar package source code analysis

Nginx reverse proxy springboot jar package source code analysis

王林
Release: 2023-05-11 20:25:21
forward
1186 people have browsed it

样例一:

server {
  listen    80;
  server_name 127.0.0.1;
  access_log logs/book.log;
  error_log logs/book.error;
 
  #将/wx-service请求转发给http://127.0.0.1:8011/wx-service处理
  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;
  }
 
  #将/bootdo请求转发给http://127.0.0.1:8012/bootdo处理
  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;
  }
 
  #将/xcloud-service请求转发给http://127.0.0.1:8013/xcloud-api处理
  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;
  }
 
  #将/eureka-service请求转发给http://127.0.0.1:8081/eureka-service处理
  location /eureka-service {
    proxy_pass http://127.0.0.1:8081/eureka-service; #这里的端口记得改成项目对应的哦
    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;
  }
 
  #将/xcloud-api请求转发给http://127.0.0.1:8082/xcloud-api处理
  location /xcloud-api {
    proxy_pass http://127.0.0.1:8082/xcloud-api; #这里的端口记得改成项目对应的哦
    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;
  }
}
Copy after login

样例二:

 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;
    }
    
    #将/wvv请求转发给http://127.0.0.1:1992/wvv处理
    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;
  }
 }
Copy after login

如果是部署多个springboot项目,则可以多次添加如下配置,只要修改成不一样的路径即可

#将/wvv请求转发给http://127.0.0.1:1991/project处理

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;
  }
Copy after login

将nginx的端口改成80端口 域名就可以直接访问 service_name可以为localhost 代表本机 localtion /xxx这块就是配置nginx端口转发 不输了几个项目就配置几个 也可以将tomcat中的项目配置在这 也是没问题的 根据上述配置 在sbin目录执行 ./nginx -s reload 刷新nginx配置就会生效了

在这提供一个spring-boot快速重启shell脚本 亲测有效

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.utf-8
 
set -m 
 
nohup java -jar -dlogging.path=$logspatch $jarname>$logspatch/catlina.out 2>&1 &
 
tail -f $logspatch/catlina.out
Copy after login

保存命名为xx.sh

建议在nginx下建立一个单独的文件夹以项目名命名,然后将jar包放入其中,再启动jar包。

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

记住 springboot项目得在配置文件中配置

server:
context-path: /xcloud-api

spring boot默认是/ 这样直接通过http://ip:port/就可以访问到index页面 但是我们要通过nginx配置多项目的话就要给每一个项目单独指定context-path

在服务器目录根据个人喜好新建一个文件夹 专门用来存放spring-boot打包成的jar 和重启脚本 类似这样

这样方便管理 logs目录是启动脚本之后生成的日志文件夹 忽略 一个项目对应一个文件夹 里面包含项目jar和一个重启shell脚本

这样就可以同时后台启动多个springboot项目并且通过一个域名来访问这些项目 如果要实时查看日志 请进入到每个项目文件的logs-目录执行

The above is the detailed content of Nginx reverse proxy springboot jar package source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template