乐趣区

关于java:使用NGINX反向代理部署Spring-Boot应用

什么是 Spring Boot

Spring Boot 通过大量的默认配置,让应用 Spring 框架进行开发变得方便快捷,从而使得 Java 开发人员专一于程序原型设计。本文介绍如何创立一个简略的 Spring Boot 利用,而后通过 NGINX 反向代理进行公布。

创立一个初始化脚本

将 Spring Boot 利用设置为服务以在服务器重启时自启动:

/etc/systemd/system/helloworld.service

[Unit] 
Description=Spring Boot HelloWorld 
After=syslog.target 
After=network.target[Service] 
User=username 
Type=simple  

[Service] 
ExecStart=/usr/bin/java -jar /home/linode/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar 
Restart=always 
StandardOutput=syslog 
StandardError=syslog 
SyslogIdentifier=helloworld  

[Install] 
WantedBy=multi-user.target

启动服务:

sudo systemctl start helloworld

查看状态是否失效:

sudo systemctl status helloworld

反向代理

当初 Spring 利用已作为服务运行,NGINX 代理容许将利用部署到非特权端口并设置 SSL。

1. 为反向代理创立 NGINX 配置文件:

/etc/nginx/conf.d/helloworld.conf 

server {        
   listen 80;        
   listen [::]:80;  
           
   server_name example.com;    
         
   location / {             
           proxy_pass http://localhost:8080/;              
           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;         
   } 
}

2. 测试配置以确保无误:

sudo nginx -t

3. 如果没有谬误,请重新启动 NGINX 以使更改失效:

sudo systemctl restart nginx

4. 当初能够通过浏览器拜访该利用。

退出移动版