In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use nginx+tomcat to achieve the separation of static and dynamic pages". In daily operation, I believe that many people have doubts about how to use nginx+tomcat to achieve the separation of static and dynamic pages. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "how to use nginx+tomcat to achieve the separation of static and dynamic pages". Next, please follow the editor to study!
Experimental environment: windows
Experimental tools: nginx, tomcat
It is very easy to install nginx under windows. Go to the official website to download the compressed package and unpack it and double-click the nginx.exe program in the directory. Then enter localhost in the browser and the following figure appears, indicating that nginx is already working.
The work flow of nginx is as follows: external, nginx is a server, all requests are first requested to nginx, then nginx distributes requests to tomcat on the intranet, and then tomcat processes the request and sends the data to nginx, and then nginx sends it to users. The feeling of the whole process to users is that nginx is processing user requests. Since this is the case, nginx definitely needs to be configured. The main configuration file is nginx.conf under the conf folder. Because I mainly separate static and dynamic files, there is no static file caching or load balancing configuration.
# user nobody;worker_processes 2 the maximum concurrency logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {# nginx default is 1024 user threads worker_connections 1024;} http {include mime.types; default_type application/octet-stream # log_format main'$remote_addr-$remote_user [$time_local] "$request" #'$status $body_bytes_sent "$http_referer" # "$http_user_agent" $http_x_forwarded_for "; # access_log logs/access.log main; sendfile on; # tcp_nopush on; # keepalive_timeout 0 # http1.1 retains the connection for a period of time after the request, so the timeout length cannot be too large or too small. # it is too small to establish a connection every time, which is a waste of system resources (users no longer request the server) keepalive_timeout 65; # gzip on; server {# nginx listening on port 80 listen 80; server_name localhost; # charset koi8-r # access_log logs/host.access.log main; # where / indicates all requests # location / {# forwards all requests from port 80 to port 8080 for processing, and proxy_pass represents the proxy path # proxy_pass http://localhost:8080; # root html; # index index.html index.htm #} # to access the project name, visit the tomcat service location / student_vote {proxy_pass http://localhost:8080;} # to access the tomcat service location ~\. (jsp | do) ${proxy_pass http://localhost:8080;} # to find location\. (js | css | png | gif) ${root f:/javaweb at the end of js, css, png and gif. (js | css | png | gif) ${root f:/javaweb } # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500502 503504 / 50x.html; location = / 50x.html {root html;} # proxy the php scripts to apache listening on 127.0.1 php$ 80 # location ~\. Php$ {# proxy_pass #} # pass the php scripts to fastcgi server listening on 127.0.0.1 pass the php scripts to fastcgi server listening on 9000 # # location ~\. Php$ {# root html; # fastcgi_pass 127.0.0.1 php$ 9000; # fastcgi_index index.php; # fastcgi_param script_filename / scripts$fastcgi_script_name; # include fastcgi_params #} # deny access to .htaccess files, if apache's document root # concurs with nginx's one # # location ~ /\ .ht {# deny all; #}} # another virtual host using mix of ip-, name-, and port-based configuration # # server {# listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / {# root html # index index.html index.htm; #} #} # https server # # server {# listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:ssl:1m; # ssl_session_timeout 5m; # ssl_ciphers highcar holl ssl_session_timeout 5m; # ssl_prefer_server_ciphers on; # location / {# root html # index index.html index.htm; #} #}}
In the above configuration, I commented out the default location /, because it intercepts all requests, whether dynamic or static, and I configure the configuration of static files to the working range of javaweb, and I will explain why.
Because the previously written project has always used jsp built-in objects for directory file access, but using nginx everything needs to be changed. When I use nginx and the project does not change the path, it is always unable to load static files. Check the log and find such an error: 18:27:30 on 2016-05-20 [error] 674806936: * 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 that according to the configuration of the files in jsp, nginx will look for static files from / stdent_vote (this is my project name) / lib/images package, and I don't want to make much changes to the project files. In fact, another way is not to use jsp's built-in objects, but to directly use http://localhost/username.png instead of built-in objects to access static files, but this will require a lot of changes. So I directly copied the lib folder under the web-inf folder to the previous folder, that is, the relationship between this folder and the web-inf folder is a sibling folder.
Through the above operation, the dynamic and static separation is achieved, there is no picture, no truth, the following shows the effect picture.
As you can see in the image above, server is "apache-coyote/1.1". This is the connector for tomcat.
The above server can see that it is nginx, indicating that the server that receives the request externally is nginx.
At this point, the study on "how to use nginx+tomcat to separate static and dynamic pages" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.