1.整体架构
本测试包括1个nginx,2个tomcat。整个系统架构如下。
Nginx下管理了静态资源,如jsp,css等。连接了2个tomcat,采用轮询访问的规则。
2.tomcat部署
本次测试中用了2个tomcat,其服务端口为8081、8082。在两个tomcat的webapps目录下部署war包。
3.Nginx配置
3.1配置服务器
# 注意,这里的server名字即org.tonny.balance不能带下划线,有下划线则不能访问 upstream org.tonny.balance { server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=1; } |
3.2设置规则
# 不带数据的请求,包括restful风格的请求 location / { proxy_pass http://org.tonny.balance; # root html; # index index.html index.htm; } # 静态文件,从这里获取 location ~* .(css|gif|png|jpeg|ico|svg)$ { root D:/App/nginx/apache-tomcat-7.0.92_1/webapps/ROOT; }
# jsp类型的请求,从这里走 location ~ .jsp$ { proxy_pass http://org.tonny.balance; } |
3.3完整文件
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream;
proxy_buffer_size 128k; proxy_buffers 32 128k; proxy_busy_buffers_size 128k; #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; keepalive_timeout 65; #gzip on; # 注意,这里的server名字即org.tonny.balance不能带下划线 upstream org.tonny.balance { server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=1; }
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
# 不带数据的请求,包括restful风格的请求 location / { proxy_pass http://org.tonny.balance; # root html; # index index.html index.htm; } # 静态文件,从这里获取 location ~* .(css|gif|png|jpeg|ico|svg)$ { root D:/App/nginx/apache-tomcat-7.0.92_1/webapps/ROOT; }
# jsp类型的请求,从这里走 location ~ .jsp$ { proxy_pass http://org.tonny.balance; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1: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 HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } |
4.测试验证
4.1tomcat验证
①在浏览器中输入:http://localhost:8081/index.jsp
②在浏览器中输入:http://localhost:8082/index.jsp
③启动nginx
输入命令:start nginx
在浏览器中输入:http://localhost/
刷新浏览器,会发现 “tomcat-test-2”字样会交替变换。
④请求部署项目
最终目的是通过nginx请求部署在tomcat中的项目
在浏览器中输入:http://localhost/suguo/cpu/list
5.文件下载
部署所用的包在https://github.com/oneqhw/spring-suguo
目录spring-suguo/src/main/resources/resource中可以下载到
名字nginx.zip,相应的sql建表语句也在这里。压缩文件中包含了nginx,tomcat,以及war包程序,解压后即可使用