nginx tomcat集群
生活随笔
收集整理的這篇文章主要介紹了
nginx tomcat集群
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
nginx tomcat group
===================================================
準備:
windows版本的nginx
兩個tomcat
1.tomcat配置:
修改tomcat的配置文件server.xml,修改端口
1.<Server port="8005" shutdown="SHUTDOWN">
? <Server port="8105" shutdown="SHUTDOWN">
2.<Connector port="8080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />
? <Connector port="8180" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />
3.<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
? <Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
建一個web項目:nginx-tomcat,加入一個test.jsp頁面,將項目分別放到兩個tomcat webapps下
tomcat1中頁面內容為:tomcat1--test.jsp
tomcat2中頁面內容為:tomcat2--test.jsp
2.nginx配置:
解壓nginx,
在conf下nginx.conf配置文件
#Nginx所用用戶和組,window下不指定 #user niumd niumd; #工作的子進程數量(通常等于CPU數量或者2倍于CPU) worker_processes 2; #錯誤日志存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #指定pid存放文件 pid logs/nginx.pid; events { #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue,window下不指定。 #use epoll; #允許最大連接數 worker_connections 2048; } 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 off; access_log logs/access.log; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 75 20; include gzip.conf; include proxy.conf; upstream localhost { #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實并不能。 #同一機器在多網情況下,路由切換,ip可能不同 #ip_hash; #weigth參數表示權值,權值越高被分配到的幾率越大 server localhost:8080 weight=1; server localhost:8180 weight=1; } server { listen 80; server_name localhost;location / { root E:/group/nginx_tomcat_Group/NginxGroup/static;index index.html index.htm; } location ~ \.(html|js|css|png|gif)$ { root E:/group/nginx_tomcat_Group/NginxGroup/static; } location ~ \.(jsp|action)$ { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost; } } } 建目錄E:/group/nginx_tomcat_Group/NginxGroup/static,(用來放靜態文件)
文件index.html,內容:nginx--index.html
文件test.html,內容:nginx--test.html
啟動兩個tomcat(bin/startup.bat),
啟動nginx(雙擊nginx.exe)
瀏覽器訪問:
1.localhost:80? ??? ??? ??? ?期望訪問到index.html:nginx--index.html
2.localhost:80/test.html?? ??? ?期望訪問到test.html:nginx--test.html
3.localhost:80/nginx-tomcat/test.jsp?? ?期望訪問到tomcat1/tomcat2:tomcat1--test.jsp/tomcat2--test.jsp
負載均衡:多次訪問localhost:80/nginx-tomcat/test.jsp,訪問tomcat1或tomcat2的概率符合nginx.conf配置文件中: #weigth配置權重
===================================================
準備:
windows版本的nginx
兩個tomcat
1.tomcat配置:
修改tomcat的配置文件server.xml,修改端口
1.<Server port="8005" shutdown="SHUTDOWN">
? <Server port="8105" shutdown="SHUTDOWN">
2.<Connector port="8080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />
? <Connector port="8180" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />
3.<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
? <Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
建一個web項目:nginx-tomcat,加入一個test.jsp頁面,將項目分別放到兩個tomcat webapps下
tomcat1中頁面內容為:tomcat1--test.jsp
tomcat2中頁面內容為:tomcat2--test.jsp
2.nginx配置:
解壓nginx,
在conf下nginx.conf配置文件
#Nginx所用用戶和組,window下不指定 #user niumd niumd; #工作的子進程數量(通常等于CPU數量或者2倍于CPU) worker_processes 2; #錯誤日志存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #指定pid存放文件 pid logs/nginx.pid; events { #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue,window下不指定。 #use epoll; #允許最大連接數 worker_connections 2048; } 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 off; access_log logs/access.log; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 75 20; include gzip.conf; include proxy.conf; upstream localhost { #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實并不能。 #同一機器在多網情況下,路由切換,ip可能不同 #ip_hash; #weigth參數表示權值,權值越高被分配到的幾率越大 server localhost:8080 weight=1; server localhost:8180 weight=1; } server { listen 80; server_name localhost;location / { root E:/group/nginx_tomcat_Group/NginxGroup/static;index index.html index.htm; } location ~ \.(html|js|css|png|gif)$ { root E:/group/nginx_tomcat_Group/NginxGroup/static; } location ~ \.(jsp|action)$ { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost; } } } 建目錄E:/group/nginx_tomcat_Group/NginxGroup/static,(用來放靜態文件)
文件index.html,內容:nginx--index.html
文件test.html,內容:nginx--test.html
啟動兩個tomcat(bin/startup.bat),
啟動nginx(雙擊nginx.exe)
瀏覽器訪問:
1.localhost:80? ??? ??? ??? ?期望訪問到index.html:nginx--index.html
2.localhost:80/test.html?? ??? ?期望訪問到test.html:nginx--test.html
3.localhost:80/nginx-tomcat/test.jsp?? ?期望訪問到tomcat1/tomcat2:tomcat1--test.jsp/tomcat2--test.jsp
負載均衡:多次訪問localhost:80/nginx-tomcat/test.jsp,訪問tomcat1或tomcat2的概率符合nginx.conf配置文件中: #weigth配置權重
3.tomcat宕機問題
proxy_connect_timeout
配置nginx連接tomcat超時時間,超時后,nginx將請求轉發給其它節點;
tomcat修復重啟后,nginx可以繼續將請求轉發給該tomcat;
可以拿上面的例子做測試。
4.反向代理
代理就代理,還不明白為什么叫反向代理。
5.session共享
可以選擇memcached、redis等緩存實現session共享;
注意:當tomcat中有定時任務跑時,看具體業務是不是定時任務只能跑一次同一時刻。
總結
以上是生活随笔為你收集整理的nginx tomcat集群的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab 正轴测图,正二轴测图的画法
- 下一篇: python动态横道图_Python数据