Springmvc nginx實(shí)現(xiàn)動(dòng)靜分離過程詳解
在下自己整理 ,如有錯(cuò)誤請指正
一般的nginx的 靜態(tài)文件的項(xiàng)目是這么配置的
location ~ .*\.(js|css)?$
{
root E:/Workspaces/Idea15/demo/web/WEB-INF;
expires 1h;
}
但是如果這樣配置,系統(tǒng)是讀取不到對應(yīng)的文件的,因?yàn)閟pringmvc本身的前端模板配置了訪問靜態(tài)資源 Handles
那如何使用nginx搭理訪問,實(shí)現(xiàn)動(dòng)靜分離
搭建nginx代理
第一階段,修改nginx.conf 文件
#location / {
#root html;
#index index.html index.htm;
#}
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://127.0.0.1:8080;
# 真實(shí)的客戶端IP
proxy_set_header X-Real-IP $remote_addr;
# 請求頭中Host信息
proxy_set_header Host $host;
# 代理路由信息,此處取IP有安全隱患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 真實(shí)的用戶訪問協(xié)議
proxy_set_header X-Forwarded-Proto $scheme;
}
這樣替換以后,就可以通過nginx 代理轉(zhuǎn)到tomcat了,但是靜態(tài)資源仍然是通過tomcat 來讀取的
注意:不需要更改原始的項(xiàng)目文件,需要修改tomcat的配置,bin/server.xml 將端口號由80改回8080
處理靜態(tài)文件
第二階段,實(shí)現(xiàn)動(dòng)靜分離
在springmvc的前端控制器中配置如下:
<mvc:resources mapping="/image/**" location="/WEB-INF/"/>
<mvc:resources mapping="/css/**" location="/WEB-INF/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/"/>
在nginx中配置如下,demo是我的項(xiàng)目名稱,以下三種方式都可以區(qū)分
location demo/image/ {
root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
location /css/ {
root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
location js/ {
root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
但是寫成 /demo/image/ 就是不行的
注:404頁面等,如果在springmvc的項(xiàng)目中配置了,就不需要在這里接著配置了

最終的nginx.conf 頁面代碼如下:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://127.0.0.1:8080;
# 真實(shí)的客戶端IP
proxy_set_header X-Real-IP $remote_addr;
# 請求頭中Host信息
proxy_set_header Host $host;
# 代理路由信息,此處取IP有安全隱患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 真實(shí)的用戶訪問協(xié)議
proxy_set_header X-Forwarded-Proto $scheme;
}
location image/ {
root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
location css/ {
root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
location js/ {
root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Nginx配合Apache或Tomcat的動(dòng)靜分離基本配置實(shí)例
- 使用Nginx+uWsgi實(shí)現(xiàn)Python的Django框架站點(diǎn)動(dòng)靜分離
- 簡單實(shí)現(xiàn)nginx+tomcat的反向代理與動(dòng)靜分離
- nginx實(shí)現(xiàn)tomcat動(dòng)靜分離詳解
- nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離
- Nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼
- 部署Nginx+Apache動(dòng)靜分離的實(shí)例詳解
- nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼
- nginx實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解
- Nginx動(dòng)靜分離實(shí)現(xiàn)案例代碼解析
- Docker Nginx容器和Tomcat容器實(shí)現(xiàn)負(fù)載均衡與動(dòng)靜分離操作
- Nginx+Tomcat實(shí)現(xiàn)負(fù)載均衡、動(dòng)靜分離的原理解析
- Nginx負(fù)載均衡以及動(dòng)靜分離的原理與配置
- nginx實(shí)現(xiàn)動(dòng)靜分離的方法示例
- nginx從安裝到配置詳細(xì)說明(安裝,安全配置,防盜鏈,動(dòng)靜分離,配置 HTTPS,性能優(yōu)化)
- Nginx+Tomcat負(fù)載均衡及動(dòng)靜分離群集的實(shí)現(xiàn)
- 服務(wù)器的負(fù)載均衡nginx+tomcat實(shí)現(xiàn)動(dòng)靜分離
- Nginx動(dòng)靜分離配置實(shí)現(xiàn)與說明
相關(guān)文章
Spring Gateway處理微服務(wù)的路由轉(zhuǎn)發(fā)機(jī)制
我們詳細(xì)地介紹了Spring Gateway,這個(gè)基于Spring 5、Spring Boot 2和Project Reactor的API網(wǎng)關(guān),通過這篇文章,我們可以清晰地看到Spring Gateway的工作原理,以及它的強(qiáng)大之處,感興趣的朋友一起看看吧2024-08-08
基于JavaMail實(shí)現(xiàn)簡單郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了基于JavaMail實(shí)現(xiàn)簡單郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Java如何導(dǎo)出數(shù)據(jù)庫中的所有數(shù)據(jù)表到指定文件夾
這篇文章主要介紹了Java導(dǎo)出數(shù)據(jù)庫中的所有數(shù)據(jù)表到指定文件夾,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

