Home  >  Article  >  Operation and Maintenance  >  How nginx realizes dynamic and static separation of tomcat

How nginx realizes dynamic and static separation of tomcat

PHPz
PHPzforward
2023-05-17 14:52:28802browse

1. Why should dynamic and static separation be achieved

1) nginx has a strong ability to process static resources

Mainly nginx processes static pages The efficiency is much higher than tomcat's processing capacity. If tomcat's request volume is 1000 times, then nginx's request volume is 6000 times. Tomcat's throughput per second is 0.6m, and nginx's throughput per second is 3.6m. It can be said that , nginx's ability to process static resources is 6 times that of tomcat, and the advantages are obvious.

2) Dynamic resources and static resources are separated to make the server structure clearer.

2. Principle of dynamic and static separation

Among the requests received by the server from the client, some are requests for static resources, such as html, css, js and Picture resources, etc., some of which are dynamic data requests. Because tomcat processes static resources relatively slowly, we can consider separating all static resources and handing them over to a server that processes static resources faster, such as nginx, and handing over dynamic requests to tomcat.

As shown in the figure below, we installed nginx and tomcat at the same time on the machine, placed all static resources under the webroot directory of nginx, and placed all dynamically requested programs in tomcat's webroot Under the directory, when the client accesses the server, if it is a request for static resources, it will go directly to nginx's webroot directory to obtain the resources. If it is a request for dynamic resources, nginx will use the principle of reverse proxy to forward the request to tomcat. Processing, thus realizing the separation of dynamic and static, and improving the performance of the server in processing requests.

How nginx realizes dynamic and static separation of tomcat

3. Detailed configuration of dynamic and static separation

1) First, familiarize yourself with nginx’s important configuration file nginx.conf

user nginx; 
worker_processes 1; 
error_log logs/error.log; 
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; 
  keepalive_timeout 65; 
  #gzip压缩功能设置 
  gzip on; 
  gzip_min_length 1k; 
  gzip_buffers  4 16k; 
  gzip_http_version 1.0; 
  gzip_comp_level 2; 
  gzip_types text/plain application/x-javascripttext/css application/xml; 
  gzip_vary on; 
  server { 
    listen    80; 
    server_name www.test.com; 
    location / { 
  #jsp网站程序根目录,一般nginx与tomcat在同一个目录 
      root /usr/local/tomcat/webapps/root; 
      index index.html index.jsp index.html; 
    } 
    location ~ .*.jsp$ { 
    index index.jsp; 
    proxy_pass http://127.0.0.1:8080;  #来自jsp请求交给tomcat处理 
    proxy_redirect off; 
    proxy_set_header host $host;  #后端的web服务器可以通过x-forwarded-for获取用户真实ip 
    proxy_set_header x-real-ip $remote_addr; 
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
    client_max_body_size 10m;  #允许客户端请求的最大单文件字节数 
    client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 
    proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时) 
    proxy_read_timeout 90;   #连接成功后,后端服务器响应时间(代理接收超时) 
    proxy_buffer_size 4k;    #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
    proxy_buffers 6 32k;    #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 
    proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2) 
    proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 
    } 
    location ~ .*\.(gif|jpg|png|bmp|swf)$  #由nginx处理静态页面 
    { 
    expires 30d;  #使用expires缓存模块,缓存到客户端30天 
    } 
    location ~ .*\.( jsp|js|css)?$ 
    { 
    expires 1d; 
    } 
    error_page 404       /404.html;  #错误页面 
    error_page  500 502 503 504 /50x.html; 
    location = /50x.html { 
      root  html; 
    } 
  }

2) Configure dynamic and static separation

#配置nginx动静分离,定义的静态页面直接从nginx发布目录读取。 
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { 
  root /webapps/myproject/code/static-resource; 
      #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 
  expires 1d; 
} 
location ~ ^/(web-inf)/ {#这个很重要,不然用户就可以访问了 
      deny all;  
    }

It should be noted here that the storage path of external static files should be consistent with the path in the request to avoid nginx splicing paths. Finally, the file cannot be found because the path does not exist. If js, css, etc. are not loaded, you can check the errorlog of nginx for debugging and correction. The log is located in the logs directory under the nginx directory. An error log is intercepted as follows:

Copy code The code is as follows:


[error] 7195#0: *1693 open() "/home/cms/include/dedeajax2.js" failed (2: no such file or directory), client: 101.226.35.225, server: localhost, request: "get /cms/include/dedeajax2.js http/1.1"

You can see that the get request is "/cms/include/dedeajax2.js", then nginx will Under /home in the configuration, look for the file with this path. The full path is:

/home/cms/include/dedeajax2.js

If the error is reported as no such file or directory, you can check the file path problem in the corresponding /home directory.

After the configuration is successful, you can find that the static files are processed through nginx, and the requests for static files no longer enter the tomcat server. Therefore, when packaging, the directories of static files such as js, css, etc. are no longer opened. Enter the war package.

The above is the detailed content of How nginx realizes dynamic and static separation of tomcat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete