springboot +nginx +freemarker 模板的简单集成

网友投稿 548 2022-05-29

刚折腾nginx 服务器  正好手头有个demo  是springboot+ freemarker    试试能不能 再结合nginx 搞一下

nginx 版本是windows版本 1.12.0

下载安装包解压到某个目录就行  最好是 盘的根目录

我的安装目录是F:\nginx-1.12.0

常用的命令   启动很简单 再安装目录下  可以点那个exe执行一下    也可以在安装的目录中用cmd命令  F:\nginx-1.12.0> /nginx      个人建议使用cmd命令

热加载命令  F:\nginx-1.12.0> /nginx -s reload     再更改nginx配置后  可以 热更新

如果配置文件有更新  建议运行一下下面的命令

F:\nginx-1.12.0>nginx -t

出现下面两行表示配置没有问题  如果出错  会有提示

nginx: the configuration file F:\nginx-1.12.0/conf/nginx.conf syntax is ok

nginx: configuration file F:\nginx-1.12.0/conf/nginx.conf test is successful

还有其他命令不再描述

本次整合 目的是   所有的展示层 放在nginx服务器下    通过nginx代理 来作为路由   来分配 访问资源的处理方式

如果是ftl  交由后台server来渲染 普通的静态资源由nginx来处理

springboot 的配置文件中  freemarker的配置   如果不集成nginx  我们一般是这样配置

springboot +nginx +freemarker 模板的简单集成

spring.freemarker.template-loader-path=classpath:/templates

绿字部分表示 你的nginx的  请求路径    在这个路径下你要确保可以访问到ftl类型的文件  然后交给后台服务器渲染处理

此处发现会抛出io异常  是因为路径的缘故   可以运行   强迫正 需要  重写  FreeMarkerAutoConfiguration

spring.freemarker.template-loader-path=http://localhost/templates/

这里是个控制器 等下要通过它来作为例子:

package com.hive.controller; import com.baomidou.mybatisplus.plugins.Page; import com.hive.domain.Area; import com.hive.service.impl.AreaServiceImpl; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; /** * Created with IntelliJ IDEA. * Author: Dax * Description: * Date: 2017/04/15 * Time: 21:09 */ @RestController @RequestMapping("/area") public class AreaRestController { @Resource private AreaServiceImpl areaServiceImpl; @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Area findAreaByCode(@PathVariable("id") String id){ return areaServiceImpl.findById(id); } @RequestMapping(value = "/{curPage}/{rows}",method = RequestMethod.GET) public Page findPage(@PathVariable("curPage") int curPage, @PathVariable("rows") int rows){ return areaServiceImpl.findPage(curPage,rows); } @RequestMapping(value = "/AreaView/{id}", method = RequestMethod.GET) public ModelAndView findArea(ModelAndView model, @PathVariable("id") String id){ Area area=areaServiceImpl.findById(id); model.addObject("area",area); model.setViewName("/pages/charts/chartjs"); return model; } }

nginx.conf  配置   优化等这里不讲

#user nobody; worker_processes 1;#推荐worker数为cpu核数,避免cpu不必要的上下文切换 events { #表示每个worker进程所能建立连接的最大值 #一个nginx最大的连接数max=worker_connections*worker_processes; #对于http请求本地资源最大并发数量为max #如果http作为反向代理,最大并发数为max/2。因为每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。 worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; #charset koi8-r; charset utf-8; access_log logs/host.access.log main; location / { root html; index index.html index.htm; } # handler freemark 下面表示 所有请求到/springboot 的都需要通过 后台服务器端来处理 记得加那个括号 此处表示 如果 通过我上面配置的server_name 来访问 http://localhost/springboot/area/AreaView/120104 其中带由/springboot 会被 下面的拦截 并由下面的代理来处理 http://localhost:8088/springboot/area/AreaView/120104 看上面的控制器就可以很好理解 location ~* /(springboot)/{ proxy_pass http://localhost:8088; } # 光由上面的是不够的 如果只配置上面 里面的css js 等静态资源的 请求地址都是 http://localhost:8088/springboot/+位置 显然不是我们 想要的 这里需要配置我们的一些静态资源 仍然由 我们的nginx来分发 配置如下 表示 凡是带由如下后缀的都通过nginx 来拦截处理 注意对里面路径的配置 如果由没有加载的 检查路径的问题 # serve static files(css|js|image..) # location ~*/.(htm|html|gif|jpg|css|js|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root html; access_log off; expires 30d; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

Nginx Spring Boot

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:最简单的 MRTs(Multi Render Targets)的完整代码示例【OpenGL】
下一篇:设计模式之学习命令模式
相关文章