1 动态页面
and 静态页面
the difference
静态资源
: When multiple users access the resource, 资源的源代码
the resource will never change.
动态资源
: When multiple users access the resources, source code resources may send change.
2 What is the static and dynamic separation
Static and dynamic separation is to make dynamic websites where dynamic pages according to certain rules to distinguish the same resources and often becomes a resource area, movement of resources to do after the split, we can according to the characteristics of static resources to do the cache operation, this is the core idea of the site static process.
Static and dynamic separation simple summary is: separation of dynamic and static files file
3 Why use static and dynamic separation
In our software development, some requests that require background processing (such as: .jsp
, .do
etc.), some do not need to request spooled (such as: css
, html
, jpg
, js
etc. file), which does not require background processing files called static files, or dynamic file. So we ignore the static background processing files. It was said that I would ignore the background static file is not finished yet. Of course, this is possible, but that the number of requests has increased significantly on the background. When we have the resources to respond to the requirements of speed, we should use this strategy to solve the separation movement.
Static and dynamic separation the website static resources ( HTML
, JavaScript
, CSS
, img
and other documents) and back-office applications deployed separately, increasing the speed of user access static code, reducing application access to the background. Here we will put a static resource nginx
, the dynamic resource forwarded to the tomcat
server.
Therefore, dynamic resource forwarded to the tomcat
server we have to use a reverse proxy in front of the talked about.
4 static and dynamic separation principle
Static and dynamic separation principle is simple, by location
request url
matching to the specific configuration is as follows:
### 静态资源访问
server {
listen 80;
server_name static.test.com;
location /static/imgs {
root F:/;
index index.html index.htm;
}
}
### 动态资源访问
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
test:
Start Nginx
the server, the browser visit: http://static.test.com/static/imgs/a.png
Find local F:/static/imgs/a.png
images, display pictures, this is a static server access.
If the JSP
introduction of the picture's address, the site can be achieved in static and dynamic separation.
5 cache static resources
Why Internet companies in the project, static resources
URL
back will add a timestamp?
effect:控制缓存
The actual project when the release version of the browser cache may be due to a conflict with the server-side code occurs.
This time you can request followed by a time stamp in a static resource, the corresponding time of each release version.
6 HTTP
304
Status Code
304
The actual status code is taking the local cache
When a client requests a file, the file cache found himself there Last Modified
, it will be included in the request If Modified Since
, the time is cached files Last Modified
. Thus, if the request contains If Modified Since
, it indicates that there is cached on the client. As long as the service side this time and modification time to determine the current request file can determine the return 304
or 200
.
For static files, such as: CSS, images, the server will automatically complete Last Modified
and If Modified Since
relatively complete or update the cache. But for dynamic page, the page is dynamically generated, often do not contain Last Modified
information, such the browser, so the gateway cache will not do, that is, each time a request is completed 200
requests.
Thus, the acceleration for dynamic pages as cache, in the first Response
of HTTP Header
increase Last Modified
defined, followed according to Request
the If Modified Since
returns and the requested content update 200
, or 304
. Although the return 304
time has made a database query, but avoid the next more database queries, and not just a return to the page content HTTP Header
, thus greatly reducing the bandwidth consumption for the user feeling is improved.