Spring Boot + Nginx 部署

lib和resources文件分离,打包之后,我们得到了三个文件:

--
 lib
 resources
 *.jar

将这三个文件拷贝到发布目录

在目录下运行命令

java -jar -Dloader.path=.,resources,lib *.jar

启动spring boot自带的tomcat服务

在nginx下面增加站点配置

server {
    listen       80; # 监听的端口
    server_name  localhost; # 域名
    index index.html; # 此处为入口文件,需要与下方保持一致
    root "/resources/static"; # 将根目录指向static目录 这样做的好处是,static目录下的静态文件,直接通过nginx代理访问

    location /demo { # 配置springboot项目的访问路径
        proxy_pass http://localhost:8080/; # 配置代理 端口与springboot的端口一致即可
        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;
        proxy_set_header Host $host;
    }
    location = /index.html { # 重写index.html
        proxy_pass http://localhost:8080/; # 指向springboot项目的默认路径
    }
}

接下来,我们需要在static目录下面创建一个空文件:index.html

就可以通过 http://localhost/ 访问了

需要注意的是,在正式发布后,记得关闭服务器的8080端口的对外访问权限,否则通过8080端口可以直接访问

本文来自博客园,作者:Bin_x,转载请注明原文链接:https://www.cnblogs.com/Bin-x/p/15353378.html

原文地址:https://www.cnblogs.com/Bin-x/p/15353378.html