关于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.当初能够通过浏览器拜访该利用。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理