The blogger has recently optimized a javaweb project. Previously, Tomcat was used to process user requests, regardless of static or dynamic content. Tomcat is mainly responsible for processing servlet files. static files are still handed over to nginx for processing. nginx processes static files a little faster than tomcat, the use of nginx greatly improves the project concurrency. The following describes the main configuration process:
Lab environment: Windows
Experimental tools: nginx and tomcat
It is very easy to install nginxin windows. You just need to download the nginx.exe program from the official website and decompress the package. Then enter localhost in the browser, indicating that nginx is already working.
The nginx workflow is as follows: for external users, nginx is a server. All requests are sent to nginx first, and then nginx distributes requests to Tomcat over the Intranet, after Tomcat processes the request, it sends the data to nginx and then nginx sends it to the user. The whole process is like nginx is processing the user request. In this case, nginx must be configured. The main configuration file is nginx In the conf folder. conf, Because I mainly perform static and dynamic separation, so no static File Cache or Server Load balancer configuration is performed.
1 # user nobody; 2 worker_processes 2; 3 4 # error_log logs/error. log; 5 # error_log logs/error. log notice; 6 # error_log logs/error. log Info; 7 8 # PID logs/nginx. PID; 9 10 11 events {12 # The default maximum number of nginx concurrent threads is 1024 user threads 13 worker_connections 1024; 14} 15 16 17 HTTP {18 include mime. types; 19 default_type application/octet-stream; 20 21 # log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "' 22 # '$ Status $ body_bytes_sent "$ http_referer" '23 #' "$ http_user_agent" "$ http_x_forwarded_for" '; 24 25 # access_log logs/access. log main; 26 27 sendfile on; 28 # tcp_nopush on; 29 30 # keepalive_timeout 0; 31 # http1.1 retains connections for a period of time after the request is complete, therefore, the timeout length here cannot be too long or too small. 32 # too small a connection should be established every time, which is too wasteful of system resources (users no longer request servers) 33 keepalive_timeout 65; 34 35 # gzip on; 36 37 server {38 # nginx listens to port 80 39 listen 80; 40 SERVER_NAME Lo Calhost; 42 # charset koi8-r; 43 44 # access_log logs/host. access. log main; 45 # Here/indicates all requests 46 # location/{47 # forward all requests from port 80 to port 8080 for processing, proxy_pass indicates the proxy path 48 # proxy_pass http: // localhost: 8080; 49 # Root HTML; 50 # index index.html index.htm; 51 #} 52 53 # access the project name to access the Tomcat service 54 location/student_vote {55 proxy_pass http: // localhost: 8080; 56} 57 58 # access the Tomcat service 59 location to the URL Ending with JSP and do ~ \. (JSP | Do) ${60 proxy_pass http: // localhost: 8080; 61} 62 63 # access the root directory for JS, CSS, PNG, and GIF to find 64 location ~ \. (JS | CSS | PNG | GIF) ${65 root F:/javaweb; 66} 67 68 69 # error_page 404/404 .html; 70 71 # redirect server error pages to the static page/50x.html 72 #73 error_page 500 502 503 504/50 x.html; 74 location =/50x.html {75 root HTML; 76} 77 78 # proxy the PHP scripts to Apache listening on 127.0.0.1: 80 79 #80 # location ~ \. Php ${81 # proxy_pass http: // 127.0.0.1; 82 #} 83 84 # pass the PHP scripts to FastCGI server listening on 127.0.0.1: 9000 85 #86 # location ~ \. PHP ${87 # Root HTML; 88 # fastcgi_pass 127.0.0.1: 9000; 89 # fastcgi_index index. PHP; 90 # fastcgi_param script_filename/scripts $ fastcgi_script_name; 91 # include fastcgi_params; 92 #} 93 94 # deny access. htaccess files, if Apache's document root 95 # concurs with nginx's one 96 #97 # location ~ /\. HT {98 # deny all; 99 #} 100} 101 102 103 # Another virtual host using mix of IP-, name -, and port-based configuration104 #105 # server {106 # Listen 8000; 107 # Listen somename: 8080; 108 # SERVER_NAME somename alias another. alias; 109 110 # location/{111 # Root HTML; 112 # index index.html index.htm; 113 #} 114 #} 115 116 117 # HTTPS server118 #119 # server {120 # Listen 443 SSL; 121 # SERVER_NAME Lo Calhost; 122 123 # ssl_certificate cert. PEM; 124 # ssl_certificate_key cert. key; 125 126 # ssl_session_cache shared: SSL: 1 m; 127 # ssl_session_timeout 5 m; 128 129 # ssl_ciphers high :! Anull :! MD5; 130 # ssl_prefer_server_ciphers on; 131 132 # location/{133 # Root HTML; 134 # index index.html index.htm; 135 #} 136 #} 137 138}
In the above configuration, I commented out the default location/, because it intercepts all requests, either dynamic or static, the other one is to configure static files into the javaweb work interval. Next I will explain why.
Because previously written projects have always used JSP built-in objects for directory file access, but everything needs to be changed when nginx is used. When nginx is used, when the project does not modify the path, static files cannot be loaded. You can view the log and find the following error: 18:27:30 [Error] 6748 #6936: * 225 createfile () "F: /javaweb/student_vote/lib/images/username.png "failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "Get/student_vote/lib/images/username.png HTTP/1.1", host: "localhost", referrer: "http: // Localhost/student_vote/index. JSP ", the general information is based on the configuration of the JSP file, nginx will find static files from the/stdent_vote (this is my project name)/lib/images package, I don't want to make too many changes to the project file. In fact, there is another way to directly use HTTP without using the built-in JSP object: // localhost/username.png is used to access static files instead of built-in objects. However, this requires many changes, so I copied the Lib folder under the web-INF folder to the previous folder, that is, the relationship between the folder and the web-INF folder is a sibling folder.
Through the above operations, the dynamic and static separation is achieved, and no picture has no truth, as shown below.
The server is "Apache-Coyote/1.1 ". This is the Tomcat Connector.
The above server shows nginx, indicating that the server receiving requests is nginx.
Use nginx + Tomcat to separate static and dynamic pages