对于静态资源比如图片,js,css等文件,我们可以在反向代理服务器nginx中进行缓存。这样浏览器在请求一个静态资源时,代理服务器nginx就可以直接处理,而不用将请求转发给后端服务器tomcat。用户请求的动态文件比如servlet,jsp则转发给Tomcat服务器处理,从而实现动静分离。这也是反向代理服务器的一个重要的作用。
服务器
服务器名称 系统版本 预装软件 IP地址
Nginx服务器 CentOS 7 Nginx 192.168.110.134
Tomcat服务器A CentOS 7 tomcat+jdk 192.168.110.129
Tomcat服务器B CentOS 7 tomcat+jdk 192.168.110.130
Tomcat服务器C CentOS 7 tomcat+jdk 192.168.110.131
新建一个jsp文件 显示服务端的ip地址 与图片
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>server index</title>
</head>
<body>
hello!
<%="server ip:"+request.getLocalAddr()%>
<p>
<img src="img/89.jpg" width="600px" height="400px" alt="烤肉">
</p>
</body>
</html>
将项目打成war包 部署到 Tomcat服务器A ,B, C中
再配置 nginx 服务器 实现负载均衡
负载均衡实现可见 https://www.jianshu.com/p/75c7b9b4007c
此外在nginx上 加上如下语句 对静态资源进行拦截
#静态交给nginx处理 注:d为day, h为hour
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root /usr/local/webapps;
expires 30d;
}
location ~ .*\.(js|css)?$
{
root /usr/local/webapps;
expires 1h;
}
nginx.conf配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#设定负载均衡的服务器列表
upstream testnginx.com {
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.110.129:8080 weight=1 max_fails=1 fail_timeout=30s;
server 192.168.110.130:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.110.131:8080 weight=1 max_fails=3 fail_timeout=30s;
}
#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 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8888;
server_name localhost;
#server_name 192.168.12.137;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://testnginx.com;
proxy_redirect default;
root html;
index index.html index.htm hello.jsp;
}
#
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root /usr/local/webapps;
expires 30d;
}
location ~ .*\.(js|css)?$
{
root /usr/local/webapps;
expires 1h;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
此时访问 会出现图片加载不出来的情况
原因为 对静态资源进行了拦截,可以将静态资源如 html js css img 等部署到nginx中 来访问
在nginx 目录 /usr/local/webapps 下新建 访问的项目名及将对应的静态目录文件拷贝到项目名下
重新再访问 ,可以再次访问静态图片了 ,至此利用nginx实现了动静分离。