使用 Nginx+Tomcat 实现动静分离

102 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第8天,点击查看活动详情

前言

客户端请求可以通过多种方式抵达服务端。最常用的是直接请求的方式,比如客户端发起一个请求,该请求可以被服务端直接处理。但除此以外,还可以在客户端和服务端中间增加一层代理,由代理层接收客户端请求,再将请求转发给服务端。其中,代理既可以是正向代理,也可以是反向代理。正向代理是为客户端做代理,相当于客户端中介,代理客户端去访问服务器。而反向代理是为服务端做代理,相当于服务端中介,代理服务器接收客户端请求。比如 Nginx 就是一款 Tomcat 服务端的反向代理服务器,Nginx 会主动代替服务端接收客户端发来的请求,然后再将此请求转交给服务端去处理。Nginx 结合 Tomcat 等服务器可以实验动静分离等功能。环境是linux

以下,通过案例演示使用 Nginx+Tomcat 实现动静分离的具体步骤。

本次使用 Ngnix 作为 Tomcat 的反向服务器,并且使用 Nginx 对所有的客户请求进行预处理:Nginx 如果发现客户端请求的是 html、png、mp3 或 mp4 等静态请求,就将请求交给自己处理;如果发现客户端请求的是 jsp 等动态请求,就将请求转交给 Tomcat 处理,如图所示:

图片描述

为了实验能正常运行,Nginx 的端口号需要改为 8080 与 Tomcat 的端口号冲突,这里将 Tomcat 的端口号改为 80.

#获取 root 权限
sudo -i
#编辑 service.xml
vim /opt/apache-tomcat-8.5.54/conf/server.xml

将 Nginx 的访问端口改为 8080。

vim /etc/nginx/conf.d/default.conf

在 /etc/nginx/nginx.conf 文件中添加 Tomcat 动态请求配置文件。

sudo vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    #开始
    upstream mytomcat {
        server 127.0.0.1:80;
    }
    #结束
    include /etc/nginx/conf.d/*.conf;
}

在 /etc/nginx/conf.d/default.conf 文件中注释原来的将动态请求配置,添加下方配置,具体操作和代码如下:

sudo vim /etc/nginx/conf.d/default.conf
server {
    listen      8080;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    #开始
    location ~ .*.(html|png|mp3|mp4)$ {
        root /usr/share/nginx/static ;
        }
    location / {
        proxy_pass http://mytomcat;
    }
    #结束

  #  location / {
  #      root   /usr/share/nginx/html;
  #      index  index.html index.htm;
  #  }

    #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   /usr/share/nginx/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;
    #}
}

在服务器中,HTML、CSS、JavaScript、图片、音频和视频等资源属于静态资源;而由 Java 编写的后台代码(如 Servlet 等)属于动态资源。Tomcat 等服务器对静态资源和动态资源的处理效率通常是不同的。一般而言,Tomcat 等服务器擅长处理动态资源,而对静态资源的处理效率较差。因此,如果能将服务器中的静态资源和动态资源相分离,只把动态请求交给 Tomcat 等服务器处理,把静态资源交给 Nginx 处理,就能大幅提高服务端的整体性能。