What is the static and dynamic separation
In order to improve the response speed of the site, reduce the load on application servers (Tomcat, Jboss, etc.), for the static resources such as images, js, css and other documents, we can be cached, so the reverse proxy server in the browser requests a static resource when the proxy server can deal directly, without forwarding the request to the backend server. Dynamic documents requested by the user, such as servlet, jsp then forwarded to Tomcat, Jboss server processing, this is the static and dynamic separation. This is also an important role in the reverse proxy server.
The main movement of this separation is achieved by nginx + tomcat, which nginx with pictures, html, JS, CSS and other static files, tomcat handle jsp, servlet and other dynamic request.
After understanding the basics, let's look to the concrete practice of the separation of static and dynamic feel. Load balancing in this blog is not introduced, but it will also configure it to achieve the effect of static and dynamic separation and load balancing.
Static and dynamic separation structure
server
Nginx server to do load balancing and static and dynamic separation, the server A, B do clusters.
Establish dynamic JSP pages
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Nginx动静分离测试</title>
</head>
<body>
<h1>您正在访问:192.168.32.229</h1>
<img src="/testweb/img/girl.jpg" alt="美女" />
</body>
</html>
jsp put testweb directory, add a picture label, load the Tomcat root directory webapps / testweb / img / girl.jpg picture files. Start test whether Tomcat can access.
Browser enter the URL: http://192.168.32.229:8080/testweb/index.jsp
Also established above 228 files and directories on the server.
To configure Nginx
For requesting access to intercept by configuring location, ①② need to be configured.
① all requests are forwarded to Tomcat for processing
LOCATION / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass HTTP: // mycluster;
# real client IP
proxy_set_header the X-Real-IP-$ REMOTE_ADDR;
# Host request header information
proxy_set_header $ Host Host;
# proxy routing information here IP has to take security risks
proxy_set_header the X-Forwarded-the For-$ proxy_add_x_forwarded_for;
# real user access protocol
proxy_set_header the X-Forwarded-Proto-$ scheme;
}
② individual requests such as: html, js, css and other static resource requests processed by Nginx
# Static files nginx to handle
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;
the Expires 30d;
}
# static files nginx to handle
LOCATION ~ * \ (JS | CSS) $..?
{
root / usr / local / webapps;
the Expires 1H;
}
root / usr / local / webapps; meaning of this code is to specify Nginx access directory, the directory that is static resource is located.
expires 30d; specify the file cache these resources at the time the client browser. 30d refers to 30 days, 1h refers to 1 hour.
Nginx complete profile
User the nobody;
worker_processes 2;
Events {
worker_connections 1024;
}
HTTP {
# set the default type binary stream
default_type file application / OCTET-Stream;
server_names_hash_bucket_size 128;
# Specify headerbuffer size from the client request header is provided to 32KB
client_header_buffer_size 32K;
# Specify the maximum number of cache size and large client request message header, here four 32KB
large_client_header_buffers 32K. 4;
# Upload file size
client_max_body_size 356m;
#nginx HttpLog the specified module, the output format designated nginx log, access output format
log_format access '$ remote_addr - $ remote_user [$ time_local] "$ request"'
'$ $ body_bytes_sent Status "$ HTTP_REFERER"'
' "$ HTTP_USER_AGENT"' "$ HTTP_X_FORWARDED_FOR";
Unknown #access log
access_log /var/log/nginx/access.log Access;
# open efficient file transfer mode, and the tcp_nopush tcp_nodelay other two fingers is set on, to prevent network congestion.
ON the sendfile;
tcp_nopush ON;
TCP_NODELAY ON;
# Set client connection keep-alive timeout
keepalive_timeout 65;
server_tokens OFF;
# client cache read request body
client_body_buffer_size 512K;
proxy_connect_timeout. 5;
proxy_send_timeout 60;
proxy_read_timeout. 5;
proxy_buffer_size 16K;
proxy_buffers. 4 64K;
128K proxy_busy_buffers_size;
proxy_temp_file_write_size 128K;
# open the gzip
the gzip ON;
# of bytes minimum allowed compression
gzip_min_length 1K;
#. 4 is a 16k memory units as a result of the compressed stream buffer
gzip_buffers 16k. 4;
# HTTP protocol version identification is provided, the default is 1.1
1.1 gzip_http_version;
#gzip compression ratio, can be provided at 1 to 9, the minimum compression ratio of the fastest speed, the maximum compression ratio is 9, the slowest, consuming the CPU
gzip_comp_level 2;
# compressed type
gzip_types text / plain application / x text-JavaScript / CSS the Application / xml;
# make front-end cache server gzip compressed mixed village after page of
gzip_vary ON;
upstream mycluster {
weight =. 1 192.168.32.229:8080 Server;
Server = weight 192.168.32.230:8080. 1;
}
Server {
the listen 8080;
server_name 192.168.32.228;
charset UTF-. 8; # Set. 8-encoded as UTF;
#location / {
# the root HTML;
# index index.html index.htm;
#}
.. # location ~ * \ (JSP | do | Action) $
LOCATION / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://mycluster;
# real client IP
the X-Real-IP-proxy_set_header $ REMOTE_ADDR;
# Host request header information
proxy_set_header $ Host Host;
# proxy routing information, where to take IP security risks
proxy_set_header the X-Forwarded-the For-$ proxy_add_x_forwarded_for;
# real user access protocol
proxy_set_header X- $ scheme Proto-Forwarded;
}
# static files nginx to handle
location ~ * \ (htm | html | gif | jpg | jpeg | png | bmp | swf | ioc | rar | zip | txt | flv | mid | doc.. | PPT | pdf | XLS | MP3 | WMA) $
{
the root / usr / local / the webapps;
the Expires 30d;
}
# static files nginx to handle
location ~ .*\.(js|css)?$
{
root /usr/local/webapps;
expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Nginx static and dynamic separation test
According to the above configuration file, and load balancing to complete the separation of the static and dynamic configuration, then start Nginx.
Visit our Web site: http://192.168.32.228:8080/testweb/index.jsp
You can see the picture is not loaded up. This is because the static resource access request has been blocked by Nginx, it is handled by Nginx. But Nginx server / usr / local / webapps directory of resources and no pictures, so the picture is not loaded up. index.jsp page can be displayed, indicating that the request has been forwarded to the dynamic of the Tomcat, Tomcat to index.jsp were resolved.
Place Picture Nginx server files in / usr / local / webapps directory, tomcat on testweb copy the entire directory into it.
Then refresh your browser again, the picture can be displayed properly.
summary
From the above example you can see that the initial realization of Nginx static and dynamic separation of functions, after the separation of static and dynamic configuration, the user requests a static resource you have defined, the default directory will publish a request to nginx, and will not request-to-back, so you can improve site response time, reduce the load pressure of the real Web server.
But in a development environment, in order to facilitate the development of our resources and static code or put together, such as the development of the test is completed, will be completed the full deployment to the build environment, however, the program code and static resources are placed respectively on different servers.
Source: https: //blog.csdn.net/zsj777/article/details/80241558