Tomcat+Nginx实现动静分离
2024-08-27 13:45:28
Tomcat是我们经常用的服务器,轻便快捷,但是数据量大的时候,会影响访问、响应速度,这时Nginx就出现了。
Nginx可做反向代理、负载均衡、动态与静态资源的分离的工作,这里我们就用它来做动静分离
动就是动态页面
静就是不改变或者不经常改变的文件,如:css、js和图片等
首先安装Nginx
下载地址http://nginx.org/en/download.html,我这里下载的是1.12.2版本,然后解压到E盘
cmd切换到nginx的目录下,输入start nginx或者nginx.exe,之后就可以在资源管理器中看到nginx.exe进程了,
直接在浏览器地址栏输入网址 http://localhost:80,回车,出现以下页面说明启动成功
配置Nginx
打开nginx文件conf下的nginx.conf配置文件,进行修改
#user nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
#请求转发到tomcat,我的端口号是8888
proxy_pass http://localhost:8888;
index ak47.html index.html index.htm;
} # 动态请求的转发
location ~ \.(jsp|do)$ {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
}
# 静态请求直接读取
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
#静态资源地址
root E:/nginx/nginx-1.12./static; #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires 1d;
}
error_page /50x.html;
location = /50x.html {
root html;
} } }
然后把项目的war放在tonmcat的webapps文件下
因为我的动态页面是放在SSM文件夹里面,所有我的静态资源也要放到
E:/nginx/nginx-1.12.2/static下的SSM文件夹下,通俗的来说就是把webapps里SSM项目下的style和public文件夹剪切到
E:/nginx/nginx-1.12.2/static的SSM下就可以了,不然无法访问静态资源。
最新文章
- java .bat批处理(java cmd命令)
- php mkdir函数
- apple 官方文档 Push Notification Programming
- iOS开发基础知识:Core Animation(核心动画)
- c#中网络异常的处理办法
- poj 2262 Goldbach's Conjecture(素数筛选法)
- php设计模式之简单工厂模式
- Andy's First Dictionary
- ios至于理解锚
- 学习的Python教程中的一些问题
- ●POJ 2284 That Nice Euler Circuit
- python -- 返回函数、匿名函数、装饰器
- 记录下自己VUE项目用Hbuider打包后启动白屏问题
- Oil Deposit
- 扁平化promise调用链(译)
- 多个string数组组装成一个List<;Object>;
- ubuntu源码安装jdk8
- Oracle存储过程的调试
- 【洛谷P2607】骑士 没有上司的舞会+
- Rpgmakermv(18)GALV RollCredits