亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Nginx+tomcat配置負載均衡

系統 2158 0

window下對Nginx+tomcat負載均衡做了配置嘗試,將全部請求轉發到tomcat,并未做靜態,動態分開,圖片防盜鏈等配置。

?

?

Nginx 介紹


???? Nginx (發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like 協議下發行。? 其特點是占有內存少,并發能力強,事實上nginx的并發能力確實在同類型的網頁伺服器中表現較好.

?

?

?

Window xp環境:Nginx+Tomcat6

1、下載地址

??????? http://nginx.org/en/download.html ,這里我們推薦下載穩定版(stable versions),本文采用nginx-1.0.4。


2、目錄結構


????? Nginx-

?????????????? |_? conf?? 配置目錄

?????????????? |_? contrib

?????????????? |_? docs 文檔目錄

?????????????? |_? logs? 日志目錄

?????????????? |_? temp 臨時文件目錄

?????????????? |_? html 靜態頁面目錄

?????????????? |_? nginx.exe 主程序


????? window下安裝Nginx極其簡單,解壓縮到一個無空格的英文目錄即可,雙擊nginx啟動,這里我安裝到:D:\software\nginx-1.0.4目錄,下面涉及到的tomcat也安裝在此目錄。

?

?? ? ?在DOS命令下即可啟動Nginx:d: ?---> ?cd d:\software\nginx-1.0.4 ---> start nginx

?? ? ? ? ?如果要對啟動的Nginx進程進行控制,也可以使用DOS命令:

?? ? ? ? ?nginx -s [stop | quit | reopen | reload]

?

3、nginx.conf配置


?? Nginx配置文件默認在conf目錄,主要配置文件為nginx.conf。下面是nginx作為前端反向代理服務器的配置。

?

?

?

Java代碼?? 收藏代碼
  1. <span>#使用的用戶和組,window下不指定??
  2. #user??www?www;??
  3. #指定工作衍生進程數(一般等于CPU總和數或總和數的兩倍,例如兩個四核CPU,則總和數為 8)??
  4. worker_processes?? 1;??
  5. #指定錯誤日志文件存放路徑,錯誤日志級別可選項為【debug|info|notice|warn|error|crit】??
  6. #error_log??logs/error.log;??
  7. #error_log??logs/error.log??notice;??
  8. error_log??logs/error.log??info;??
  9. #指定pid存放路徑??
  10. pid????????logs/nginx.pid;??
  11. ??
  12. #工作模式及連接數上限???
  13. events?{??
  14. ????#使用網絡I/O模型,Linux系統推薦使用epoll模型,FreeBSD系統推薦使用kqueue;window下不指定??
  15. ????#use?epoll;??
  16. ????#允許的連接數??
  17. ????worker_connections?? 1024;??
  18. }??
  19. ??
  20. #設定http服務器,利用他的反向代理功能提供負載均衡支持???
  21. http?{??
  22. ????#設定mime類型???
  23. ????include???????mime.types;??
  24. ????default_type??application/octet-stream;??
  25. ??
  26. ????#設定日志格式???
  27. ????#log_format??main?? '$remote_addr?-?$remote_user?[$time_local]?"$request"?'??
  28. ????#?????????????????? '$status?$body_bytes_sent?"$http_referer"?'??
  29. ????#?????????????????? '"$http_user_agent"?"$http_x_forwarded_for"';??
  30. ??
  31. ????log_format?main? '$remote_addr?-?$remote_user?[$time_local]'?????
  32. ???????????????????????????????????????? '"$request"?$status?$bytes_sent'?????
  33. ???????????????????????????????????????? '"$http_referer"?"$http_user_agent"?"$http_x_forwarded_for"'?????
  34. ???????????????????????????????????????? '"$gzip_ratio"';?????
  35. ????????log_format?download? '$remote_addr?-?$remote_user?[$time_local]'?????
  36. ???????????????????????????????????????? '"$request"?$status?$bytes_sent'?????
  37. ???????????????????????????????????????? '"$http_referer"?"$http_user_agent"'?????
  38. ???????????????????????????????????????? '"$http_range"?"$sent_http_content_range"';??
  39. ??????
  40. ????#設定請求緩沖?????
  41. ????client_header_buffer_size?1k;?????
  42. ????large_client_header_buffers? 4?4k;??
  43. ??????
  44. ????#設定access?log????
  45. ????access_log??logs/access.log??main;??
  46. ????client_header_timeout?3m;?????
  47. ????????client_body_timeout?3m;?????
  48. ????????send_timeout?3m;???
  49. ??
  50. ????sendfile????????on;??
  51. ????tcp_nopush?????on;??
  52. ????tcp_nodelay?on;????
  53. ????#keepalive_timeout?? 0;??
  54. ????keepalive_timeout?? 65;??
  55. ??
  56. ????#開啟gzip模塊???
  57. ????gzip??on;??
  58. ????gzip_min_length? 1100;?????
  59. ????????gzip_buffers? 4?8k;?????
  60. ????????gzip_types?text/plain?application/x-javascript?text/css?application/xml;????
  61. ???????????
  62. ????????output_buffers? 1?32k;?????
  63. ????????postpone_output? 1460;??
  64. ??????
  65. ????server_names_hash_bucket_size? 128;????
  66. ????client_max_body_size?8m;????
  67. ??????
  68. ????fastcgi_connect_timeout? 300;????
  69. ????fastcgi_send_timeout? 300;????
  70. ????fastcgi_read_timeout? 300;????
  71. ????fastcgi_buffer_size?64k;????
  72. ????fastcgi_buffers? 4?64k;????
  73. ????fastcgi_busy_buffers_size?128k;????
  74. ????fastcgi_temp_file_write_size?128k;????
  75. ????gzip_http_version? 1.1;????
  76. ????gzip_comp_level? 2;????
  77. ????gzip_vary?on;???
  78. ??
  79. ????#設定負載均衡的服務器列表?????
  80. ????????upstream?localhost?{???
  81. ????????????#根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實并不能。????
  82. ????????#同一機器在多網情況下,路由切換,ip可能不同??????
  83. ????????????#weigth參數表示權值,權值越高被分配到的幾率越大?????
  84. ????????????server?localhost: 8080?weight= 1;?????
  85. ????????????server?localhost: 9080?weight= 1;?????
  86. ????????}???
  87. ??
  88. ????#設定虛擬主機??
  89. ????server?{??
  90. ????????listen??????? 80;??
  91. ????????server_name??localhost;??
  92. ??
  93. ????????#koi8-r??
  94. ????????charset?UTF- 8;??
  95. ??
  96. ????????#設定本虛擬主機的訪問日志??
  97. ????????access_log??logs/host.access.log??main;??
  98. ??????????
  99. ????????#假如訪問?/img/*,?/js/*,?/css/*?資源,則直接取本地文檔,不通過squid?????
  100. ????????#假如這些文檔較多,不推薦這種方式,因為通過squid的緩存效果更好????
  101. ????????location?~?^/(img|js|css)/?{?????
  102. ????????????????????root?/data3/Html;?????
  103. ????????????????????expires?24h;?????
  104. ????????????????}???
  105. ??????????????????
  106. ????????????????#對? "/"?啟用負載均衡??
  107. ????????location?/?{??
  108. ????????????root???html;??
  109. ????????????index??index.html?index.htm?index.jsp;??
  110. ??????????????
  111. ????????????????????????proxy_redirect?off;???
  112. ????????????????????????#保留用戶真實信息????
  113. ????????????????????????proxy_set_header?Host?$host;?????
  114. ????????????????????????proxy_set_header?X-Real-IP?$remote_addr;?????
  115. ????????????????????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;???
  116. ????????????????????????#允許客戶端請求的最大單個文件字節數????
  117. ????????????????????????client_max_body_size?10m;?????
  118. ????????????????????????#緩沖區代理緩沖用戶端請求的最大字節數,可以理解為先保存到本地再傳給用戶??
  119. ????????????????????????client_body_buffer_size?128k;?????
  120. ????????????????????????#跟后端服務器連接超時時間?發起握手等候響應超時時間??
  121. ????????????????????????proxy_connect_timeout? 90;??
  122. ????????????????????????#連接成功后?等待后端服務器響應時間?其實已進入后端的排隊之中等候處理???
  123. ????????????????????????proxy_read_timeout? 90;?????
  124. ??????????????????????#后端服務器數據回傳時間?就是在規定時間內后端服務器必須傳完所有數據?????
  125. ????????????????????????proxy_send_timeout? 90;??
  126. ????????????????????????#代理請求緩存區?這個緩存區間會保存用戶的頭信息一共Nginx進行規則處理?一般只要能保存下頭信息即可??
  127. ????????????????????????proxy_buffer_size?4k;?????
  128. ????????????????????????#同上?告訴Nginx保存單個用的幾個Buffer最大用多大空間??
  129. ????????????????????????proxy_buffers? 4?32k;?????
  130. ????????????????????????#如果系統很忙的時候可以申請國內各大的proxy_buffers?官方推薦?* 2??
  131. ????????????????????????proxy_busy_buffers_size?64k;????
  132. ????????????????????????#proxy?緩存臨時文件的大小???
  133. ????????????????????????proxy_temp_file_write_size?64k;???
  134. ????????????????????????proxy_next_upstream?error?timeout?invalid_header?http_500?http_503?http_404;??
  135. ????????????????????????proxy_max_temp_file_size?128m;???
  136. ??????????????????????????
  137. ????????????????????????proxy_pass?http: //localhost;??
  138. ????
  139. ????????}??
  140. ??
  141. ????????#設定查看Nginx狀態的地址?????
  142. ????????????????#location?/NginxStatus?{?????
  143. ????????????????#???????stub_status?on;?????
  144. ????????????????#???????access_log?on;?????
  145. ????????????????#???????auth_basic? "NginxStatus";?????
  146. ????????????????#???????auth_basic_user_file?conf/htpasswd;?????
  147. ????????????????#}?????
  148. ??
  149. ????????#error_page?? 404??????????????/ 404.html;??
  150. ??
  151. ????????#?redirect?server?error?pages?to?the? static?page?/50x.html??
  152. ????????#??
  153. ??????????
  154. ????????error_page??? 500? 502? 503? 504??/50x.html;??
  155. ????????location?=?/50x.html?{??
  156. ????????????root???html;??
  157. ????????}??
  158. ??
  159. ????????#?proxy?the?PHP?scripts?to?Apache?listening?on? 127.0. 0.1: 80??
  160. ????????#??
  161. ????????#location?~?\.php$?{??
  162. ????????#????proxy_pass???http: //127.0.0.1;??
  163. ????????#}??
  164. ??
  165. ????????#?pass?the?PHP?scripts?to?FastCGI?server?listening?on? 127.0. 0.1: 9000??
  166. ????????#??
  167. ????????#location?~?\.php$?{??
  168. ????????#????root???????????html;??
  169. ????????#????fastcgi_pass??? 127.0. 0.1: 9000;??
  170. ????????#????fastcgi_index??index.php;??
  171. ????????#????fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;??
  172. ????????#????include????????fastcgi_params;??
  173. ????????#}??
  174. ??
  175. ????????#?deny?access?to?.htaccess?files,? if?Apache's?document?root??
  176. ????????#?concurs?with?nginx's?one??
  177. ????????#??
  178. ????????#location?~?/\.ht?{??
  179. ????????#????deny??all;??
  180. ????????#}??
  181. ????}??
  182. ??
  183. ??
  184. ????#?another?virtual?host?using?mix?of?IP-,?name-,?and?port-based?configuration??
  185. ????#??
  186. ????#server?{??
  187. ????#????listen??????? 8000;??
  188. ????#????listen???????somename: 8080;??
  189. ????#????server_name??somename??alias??another.alias;??
  190. ??
  191. ????#????location?/?{??
  192. ????#????????root???html;??
  193. ????#????????index??index.html?index.htm;??
  194. ????#????}??
  195. ????#}??
  196. ??
  197. ??
  198. ????#?HTTPS?server??
  199. ????#??
  200. ????#server?{??
  201. ????#????listen??????? 443;??
  202. ????#????server_name??localhost;??
  203. ??
  204. ????#????ssl??????????????????on;??
  205. ????#????ssl_certificate??????cert.pem;??
  206. ????#????ssl_certificate_key??cert.key;??
  207. ??
  208. ????#????ssl_session_timeout??5m;??
  209. ??
  210. ????#????ssl_protocols??SSLv2?SSLv3?TLSv1;??
  211. ????#????ssl_ciphers??ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;??
  212. ????#????ssl_prefer_server_ciphers???on;??
  213. ??
  214. ????#????location?/?{??
  215. ????#????????root???html;??
  216. ????#????????index??index.html?index.htm;??
  217. ????#????}??
  218. ????#}??
  219. ??
  220. }??
  221. </span>??

?

?

?

??4、Tomcat配置

?

?? ? ?tomcat的配置依然使用 Apache+tomcat集群配置的中配置。

?

?

5、驗證配置與測試負載均衡


???? 首先測試nginx配置是否正確,測試命令:nginx -t? (默認驗證:conf\nginx.conf),也可以指定配置文件路徑。

?

?? ?最后驗證配置負載均衡設置, http://localhost/ http://localhost/index.jsp

?

?? ?測試負載均衡 參考? Apache + Tomcat集群配置詳解(2)

?

?

至此window下nginx+tomcat負載均衡配置結束,關于tomcat Session的問題通常是采用memcached,或者采用nginx_upstream_jvm_route ,他是一個 Nginx 的擴展模塊,用來實現基于 Cookie 的 Session Sticky 的功能。如果tomcat過多不建議session同步,server間相互同步session很耗資源,高并發環境容易引起Session風暴。請根據自己應用情況合理采納session解決方案。

?

?

下面幾篇不錯的文章:

?? ??http://czllfy.iteye.com/blog/510295

?? ??http://www.blogjava.net/Alpha/archive/2011/06/21/352745.html

?? ??http://www.jtben.com/document/4440

?? ? http://tmsoft.lsxy.com/index.php?load=read&id=938

?

張宴的Blog:http://blog.s135.com/nginx_cache/

Nginx+tomcat配置負載均衡


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线观看亚洲精品国产 | 夜色私人影院永久地址入口 | 四虎在线永久 | 欧美一级艳片视频免费观看 | 色中文字幕在线 | 成人免费观看网欧美片 | 四虎成人精品在永久免费 | 在线综合色 | 亚洲乱码中文字幕久久 | 久久这里只有精品9 | 男人的天堂黄 | 国产精品成人在线播放 | 狠狠狠色丁香婷婷综合久久五月 | 欧美日韩亚洲m码色帝国 | 手机看片日韩 | 国产精品乱码在线观看 | 久久精品国产三级不卡 | 国产在线一区二区三区欧美 | 亚洲激情在线观看 | 成人影院久久久久久影院 | 国产成人黄色在线观看 | 免费观看美女光子让男人玩 | 免费一级欧美大片在线观看 | 男人都懂的网址 | 激情五月色婷婷在线观看 | 久艹在线播放 | 久久天天 | 亚洲欧美成人中文在线网站 | 最近中文字幕在线视频1 | 国产99在线观看 | 麻豆91精品91久久久 | 麻豆久久婷婷综合五月国产 | 婷婷综合五月中文字幕欧美 | 国产精品久久久久久久久久一区 | jizzjizz欧美69巨大 | 高清性色生活片久久久 | 国产成人经典三级在线观看 | 久久桃花综合 | 免费黄色在线 | 日韩精品麻豆 | 中文字幕视频在线观看 |