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.
3. Detailed configuration of dynamic and static separation
1) First, familiarize yourself with nginx’s important configuration file nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
2) Configure dynamic and static separation
1 2 3 4 5 6 7 8 9 |
|
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:
1 |
|
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!