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包程序,解壓后即可使用