使用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 -t3.如果没有错误,请重新启动NGINX以使更改生效:
sudo systemctl restart nginx4.现在可以通过浏览器访问该应用。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论