1-- Nginx server] [High Performance - 11 Nginx + Tomcat static and dynamic separation

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, .doetc.), some do not need to request spooled (such as: css, html, jpg, jsetc. 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, imgand 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 tomcatserver.

Therefore, dynamic resource forwarded to the tomcatserver 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 locationrequest urlmatching 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 Nginxthe server, the browser visit: http://static.test.com/static/imgs/a.png

Find local F:/static/imgs/a.pngimages, display pictures, this is a static server access.

If the JSPintroduction 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 URLback 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 304Status 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 304or 200.

For static files, such as: CSS, images, the server will automatically complete Last Modifiedand If Modified Sincerelatively complete or update the cache. But for dynamic page, the page is dynamically generated, often do not contain Last Modifiedinformation, such the browser, so the gateway cache will not do, that is, each time a request is completed 200requests.

Thus, the acceleration for dynamic pages as cache, in the first Responseof HTTP Headerincrease Last Modifieddefined, followed according to Requestthe If Modified Sincereturns and the requested content update 200, or 304. Although the return 304time 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.

Published 675 original articles · won praise 214 · Views 140,000 +

Guess you like

Origin blog.csdn.net/weixin_42112635/article/details/104977964