Tomcat+Nginx实现动静分离

Tomcat是咱们常常用的服务器,轻便快捷,可是数据量大的时候,会影响访问、响应速度,这时Nginx就出现了。css

Nginx可作反向代理、负载均衡、动态与静态资源的分离的工做,这里咱们就用它来作动静分离html

动就是动态页面nginx

静就是不改变或者不常常改变的文件,如:css、js和图片等web

首先安装Nginx浏览器

下载地址http://nginx.org/en/download.html,我这里下载的是1.12.2版本,而后解压到E盘缓存

cmd切换到nginx的目录下,输入start nginx或者nginx.exe,以后就能够在资源管理器中看到nginx.exe进程了,tomcat

直接在浏览器地址栏输入网址 http://localhost:80,回车,出现如下页面说明启动成功服务器

配置Nginxapp

打开nginx文件conf下的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;

    #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       80;
        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.2/static; #expires定义用户浏览器缓存的时间为7天,若是静态页面不常更新,能够设置更长,这样能够节省带宽和缓解服务器的压力 expires 1d; } error_page 500 502 503 504 /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下就能够了,否则没法访问静态资源。
相关文章
相关标签/搜索