springboot +nginx +freemarker 模板的簡單集成
剛折騰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? 我們一般是這樣配置
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小時內刪除侵權內容。