2008年的新浪播客由靜態伺服器叢集和動態伺服器叢集兩部分組成,靜態伺服器叢集採用Squid做前端緩存,動態伺服器也稱為介面伺服器,主要用來實現顯示播放數,記錄播放日誌等等。
介面伺服器上採用F5 BIG-IP硬體四/七層負載平衡交換機,對4台Nginx反向代理伺服器進行四層負載平衡,由這四台nginx伺服器判斷URL,進行分組,對後端3組web伺服器進行七層負載平衡。
F5 BIG-IP後端的3組web伺服器,配置不一樣,第一組記憶體密集型,技術主要是PHP+Mencache服務; 第2組為CPU密集型服務,主要消耗CP資源;第三組為磁碟密集型,為記錄日誌等操作,要求大空間。
以下程式碼為介面伺服器Nginx負載平衡設定;
<code><span>/***代码是N年前新浪接口服务器的nginx负载均衡配置,提供按URL分组服务,负载均衡服务***/</span>usr www www; worker_processes <span>10</span>; error_log /data1/logs/nginx_error.log crit; pid /tmp/nginx.pid worker_rlimt_nofile <span>51200</span>; events { <span>use</span><span>epull</span>; worker_connections <span>51200</span>; } http { <span>include</span> conf/mine.types; default_type application/octet-stream; charset gb2312; server_name_hash_bucket_size <span>128</span>; keeplaive_timeout <span>15</span>; sendfile on; tcp_mopush on; tcp_nodelay on; <span>#第一组接口机:Memcache相关(点击数)</span> upstream count.<span>interface</span>.video.sian.com.cn{ server xx.xx.xx<span>.55</span>:<span>80</span>; server xx.xx.xx<span>.58</span>:<span>80</span>; server xx.xx.xx<span>.47</span>:<span>80</span>; } <span>#第二组接口机:提供数据类程序</span> upstream data.<span>interface</span>.video.sian.com.cn{ server xx.xx.xx<span>.59</span>:<span>80</span>; server xx.xx.xx<span>.64</span>:<span>80</span>; server xx.xx.xx<span>.48</span>:<span>80</span>; } <span>#第三组接口机:打日志类程序,功能相关,嵌套页面</span> upstream log.<span>interface</span>.video.sian.com.cn{ server xx.xx.xx<span>.72</span>:<span>80</span>; server xx.xx.xx<span>.49</span>:<span>80</span>; } } server { listen <span>80</span>; server_name <span><span>interface</span>.<span>video</span>.<span>sian</span>.<span>com</span>.<span>cn</span>; <span>location</span> / {</span> proxy_redirect off; <span>#后端的web服务器可以直接通过X-Forward-For 获取用户真实ip</span> proxy_set_header X-Forward-<span>For</span><span>$remote_adr</span>; <span>#按URL进行分组,第一组:Memcache相关(点击数)</span><span>if</span> (<span>$request_uri</span> ~ <span>"^\/app\/count\/"</span>) { proxy_pass http:<span>//count.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/app\/online\/"</span>) { proxy_pass http:<span>//count.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/user\/getLoginGap.php"</span>) { proxy_pass http:<span>//count.interface.video.sian.com.cn;</span> } <span>#按URL进行分组,第二组:外部提供数据类程序</span><span>if</span> (<span>$request_uri</span> ~ <span>"^\/crossdomain.xml"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/client\/topVideoClient.php"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/common\/"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/randplay\/randplay.php"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/topic\/suggTop.php"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/uploadClient\/"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/xml\/"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/outinterface\/"</span>) { proxy_pass http:<span>//data.interface.video.sian.com.cn;</span> } <span>#按URL进行分组,第三组:打日志类程序</span><span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/flash\/"</span>) { proxy_pass http:<span>//log.interface.video.sian.com.cn;</span> } <span>if</span> (<span>$request_uri</span> ~ <span>"^\/interface\/playrank\/playrank2008_10.php"</span>) { proxy_pass http:<span>//log.interface.video.sian.com.cn;</span> } <span>#按URL进行分组,其他组:功能相关,嵌套页面等未匹配到的URL</span> proxy_pass http:<span>//log.interface.video.sina.com.cn;</span> } <span>#定义日至格式</span> log_format count <span>'$remote_addr - $remote_user [$time_local] $request'</span><span>'"$status" $body_bytes_sent "$http_referer"'</span><span>'"$http_user_agent" "$http_x_forwarded_for"'</span>; <span>#打日志</span> access_log /data1/logs/<span><span>interface</span>.<span>log</span><span>count</span>; #允许客户端请求的最大单文件字节数 <span>client_max_body_size</span> 10<span>m</span>; #缓冲区代理缓冲用户端的最大字节数 可以理解为现存到本地再传给用户 <span>client_body_size</span> 128<span>k</span>; #跟后端服务器连接的超时时间<span>_</span>发起握手等候响应超时时间 <span>proxy_connect_time</span> 600; #连接成功后<span>_</span>等待后端服务器响应时间<span>_</span>其实已经进入后端的派对等候处理 <span>proxy_read_timeout</span> 600; #后端回传时间<span>_</span>规定时间内传完所有数据 <span>proxy_send_timeout</span> 600; #代理请求缓存区,保存用户的头信息以供<span>Nginx</span>进行规则处理 <span>proxy_buffer_size</span> 8<span>k</span>; <span>proxy_buffers</span> 4 32<span>k</span>; <span>proxy_busy_buffers_size</span> 64<span>k</span>; <span>proxy_temp_file_write_size</span> 64<span>k</span>; } }</span></code>
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
以上就介紹了N年前Nginx負載平衡在新浪播客中的應用,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。