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作為前端反向代理服務器的配置。
?
?
?
- <span>#使用的用戶和組,window下不指定??
- #user??www?www;??
- #指定工作衍生進程數(一般等于CPU總和數或總和數的兩倍,例如兩個四核CPU,則總和數為 8)??
- worker_processes?? 1;??
- #指定錯誤日志文件存放路徑,錯誤日志級別可選項為【debug|info|notice|warn|error|crit】??
- #error_log??logs/error.log;??
- #error_log??logs/error.log??notice;??
- error_log??logs/error.log??info;??
- #指定pid存放路徑??
- pid????????logs/nginx.pid;??
- ??
- #工作模式及連接數上限???
- events?{??
- ????#使用網絡I/O模型,Linux系統推薦使用epoll模型,FreeBSD系統推薦使用kqueue;window下不指定??
- ????#use?epoll;??
- ????#允許的連接數??
- ????worker_connections?? 1024;??
- }??
- ??
- #設定http服務器,利用他的反向代理功能提供負載均衡支持???
- http?{??
- ????#設定mime類型???
- ????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"';??
- ??
- ????log_format?main? '$remote_addr?-?$remote_user?[$time_local]'?????
- ???????????????????????????????????????? '"$request"?$status?$bytes_sent'?????
- ???????????????????????????????????????? '"$http_referer"?"$http_user_agent"?"$http_x_forwarded_for"'?????
- ???????????????????????????????????????? '"$gzip_ratio"';?????
- ????????log_format?download? '$remote_addr?-?$remote_user?[$time_local]'?????
- ???????????????????????????????????????? '"$request"?$status?$bytes_sent'?????
- ???????????????????????????????????????? '"$http_referer"?"$http_user_agent"'?????
- ???????????????????????????????????????? '"$http_range"?"$sent_http_content_range"';??
- ??????
- ????#設定請求緩沖?????
- ????client_header_buffer_size?1k;?????
- ????large_client_header_buffers? 4?4k;??
- ??????
- ????#設定access?log????
- ????access_log??logs/access.log??main;??
- ????client_header_timeout?3m;?????
- ????????client_body_timeout?3m;?????
- ????????send_timeout?3m;???
- ??
- ????sendfile????????on;??
- ????tcp_nopush?????on;??
- ????tcp_nodelay?on;????
- ????#keepalive_timeout?? 0;??
- ????keepalive_timeout?? 65;??
- ??
- ????#開啟gzip模塊???
- ????gzip??on;??
- ????gzip_min_length? 1100;?????
- ????????gzip_buffers? 4?8k;?????
- ????????gzip_types?text/plain?application/x-javascript?text/css?application/xml;????
- ???????????
- ????????output_buffers? 1?32k;?????
- ????????postpone_output? 1460;??
- ??????
- ????server_names_hash_bucket_size? 128;????
- ????client_max_body_size?8m;????
- ??????
- ????fastcgi_connect_timeout? 300;????
- ????fastcgi_send_timeout? 300;????
- ????fastcgi_read_timeout? 300;????
- ????fastcgi_buffer_size?64k;????
- ????fastcgi_buffers? 4?64k;????
- ????fastcgi_busy_buffers_size?128k;????
- ????fastcgi_temp_file_write_size?128k;????
- ????gzip_http_version? 1.1;????
- ????gzip_comp_level? 2;????
- ????gzip_vary?on;???
- ??
- ????#設定負載均衡的服務器列表?????
- ????????upstream?localhost?{???
- ????????????#根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實并不能。????
- ????????#同一機器在多網情況下,路由切換,ip可能不同??????
- ????????????#weigth參數表示權值,權值越高被分配到的幾率越大?????
- ????????????server?localhost: 8080?weight= 1;?????
- ????????????server?localhost: 9080?weight= 1;?????
- ????????}???
- ??
- ????#設定虛擬主機??
- ????server?{??
- ????????listen??????? 80;??
- ????????server_name??localhost;??
- ??
- ????????#koi8-r??
- ????????charset?UTF- 8;??
- ??
- ????????#設定本虛擬主機的訪問日志??
- ????????access_log??logs/host.access.log??main;??
- ??????????
- ????????#假如訪問?/img/*,?/js/*,?/css/*?資源,則直接取本地文檔,不通過squid?????
- ????????#假如這些文檔較多,不推薦這種方式,因為通過squid的緩存效果更好????
- ????????location?~?^/(img|js|css)/?{?????
- ????????????????????root?/data3/Html;?????
- ????????????????????expires?24h;?????
- ????????????????}???
- ??????????????????
- ????????????????#對? "/"?啟用負載均衡??
- ????????location?/?{??
- ????????????root???html;??
- ????????????index??index.html?index.htm?index.jsp;??
- ??????????????
- ????????????????????????proxy_redirect?off;???
- ????????????????????????#保留用戶真實信息????
- ????????????????????????proxy_set_header?Host?$host;?????
- ????????????????????????proxy_set_header?X-Real-IP?$remote_addr;?????
- ????????????????????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;???
- ????????????????????????#允許客戶端請求的最大單個文件字節數????
- ????????????????????????client_max_body_size?10m;?????
- ????????????????????????#緩沖區代理緩沖用戶端請求的最大字節數,可以理解為先保存到本地再傳給用戶??
- ????????????????????????client_body_buffer_size?128k;?????
- ????????????????????????#跟后端服務器連接超時時間?發起握手等候響應超時時間??
- ????????????????????????proxy_connect_timeout? 90;??
- ????????????????????????#連接成功后?等待后端服務器響應時間?其實已進入后端的排隊之中等候處理???
- ????????????????????????proxy_read_timeout? 90;?????
- ??????????????????????#后端服務器數據回傳時間?就是在規定時間內后端服務器必須傳完所有數據?????
- ????????????????????????proxy_send_timeout? 90;??
- ????????????????????????#代理請求緩存區?這個緩存區間會保存用戶的頭信息一共Nginx進行規則處理?一般只要能保存下頭信息即可??
- ????????????????????????proxy_buffer_size?4k;?????
- ????????????????????????#同上?告訴Nginx保存單個用的幾個Buffer最大用多大空間??
- ????????????????????????proxy_buffers? 4?32k;?????
- ????????????????????????#如果系統很忙的時候可以申請國內各大的proxy_buffers?官方推薦?* 2??
- ????????????????????????proxy_busy_buffers_size?64k;????
- ????????????????????????#proxy?緩存臨時文件的大小???
- ????????????????????????proxy_temp_file_write_size?64k;???
- ????????????????????????proxy_next_upstream?error?timeout?invalid_header?http_500?http_503?http_404;??
- ????????????????????????proxy_max_temp_file_size?128m;???
- ??????????????????????????
- ????????????????????????proxy_pass?http: //localhost;??
- ????
- ????????}??
- ??
- ????????#設定查看Nginx狀態的地址?????
- ????????????????#location?/NginxStatus?{?????
- ????????????????#???????stub_status?on;?????
- ????????????????#???????access_log?on;?????
- ????????????????#???????auth_basic? "NginxStatus";?????
- ????????????????#???????auth_basic_user_file?conf/htpasswd;?????
- ????????????????#}?????
- ??
- ????????#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;??
- ????#????server_name??localhost;??
- ??
- ????#????ssl??????????????????on;??
- ????#????ssl_certificate??????cert.pem;??
- ????#????ssl_certificate_key??cert.key;??
- ??
- ????#????ssl_session_timeout??5m;??
- ??
- ????#????ssl_protocols??SSLv2?SSLv3?TLSv1;??
- ????#????ssl_ciphers??ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;??
- ????#????ssl_prefer_server_ciphers???on;??
- ??
- ????#????location?/?{??
- ????#????????root???html;??
- ????#????????index??index.html?index.htm;??
- ????#????}??
- ????#}??
- ??
- }??
- </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/
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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