使用nginx进行负载均衡
nginx主要用于1:請求分流?2:負載均衡。用在大型系統(集群)上,在單機上體現不出優勢。?
?
本實例在windows環境下進行。?
?
一、安裝nginx?
?
1、下載nginx1.8.0版?
2、解壓至1、下載nginx1.8.0版?
2、解壓至c:\nginx-1.8.0?
3、在cmd控制臺進入到c:\nginx-1.8.0目錄?
4、運行start?nginx啟動?nginx服務器?
5、在瀏覽器中輸入?localhost?,如果能打開nginx歡迎頁面,說明啟動成功。?
?
相關命令:?
nginx?-s?stop?????quick?exit?
nginx?-s?quit?????graceful?quit?
nginx?-s?reload?????changing?configuration,?starting?a?new?worker,?quitting?an?old?worker?gracefully?
nginx?-s?reopen?????reopening?log?files?
tasklist?/fi?"imagename?eq?nginx.exe"?查看啟動的nginx進程?
3、在cmd控制臺進入到c:\nginx-1.8.0目錄?
4、運行start?nginx啟動?nginx服務器?
5、在瀏覽器中輸入?localhost?,如果能打開nginx歡迎頁面,說明啟動成功。?
?
相關命令:?
nginx?-s?stop?????quick?exit?
nginx?-s?quit?????graceful?quit?
nginx?-s?reload?????changing?configuration,?starting?a?new?worker,?quitting?an?old?worker?gracefully?
nginx?-s?reopen?????reopening?log?files?
tasklist?/fi?"imagename?eq?nginx.exe"?查看啟動的nginx進程?
?
二、部署項目?
?
基本思路?
?
????nginx用于處理靜態頁面和請求分流調度。?
?
????1、在nginx安裝完畢后,在nginx.conf中配置D:\dev\nginx作為nginx的執行目錄(機器ip:192.168.11.35):如下?
????????server?{?
????????????listen???????localhost:8888;?#8888為端口號?
????????????root?????????D:/dev/nginx/myspring;#myspring是項目名稱?
????????????index?????????test.html;?
????????????#jsp動態頁面由此proxy_pass處理?
????????????#location?~?\.jsp$?{?
????????????#????root???html;?
????????????#????index??index.html?index.htm;?
????????????#????proxy_pass?http://192.168.11.35:8080;?
????????????#}?
????????}?
????打開靜態頁面:重啟nginx服務器后,訪問http://localhost:8888/test.html將顯示test.html中的內容。?
?????
????2、在windows中使用tomcat部署一份myspring項目,ip為192.168.11.35,端口號設為8080?
????3、在linux中使用tomcat部署一份myspring項目,ip為192.168.11.38,端口號為8181?
????4、配置負載均衡:?
????????在http{}中配置?
????????#負載均衡的兩臺機器?
????????upstream?myCluster?{?
????????????server?192.168.11.35:8080;?#1/6的請求將會被分配到此服務器?
????????????server?192.168.11.38:8181?weight=5;?#權重?指5/6的請求將會被分配到此服務器?
????????}??
????????在server?{}中配置?
????????location?~?\.jsp$?{?
????????????proxy_pass?http://myCluster?;?#這里的名字和上面的cluster的名字相同?
????????????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;?
?????????????proxy_connect_timeout???1;?#宕機時超時時間
?????????????proxy_send_timeout??????1;
?????????????proxy_read_timeout??????1;
????????}??
????????以上配置表示訪問html靜態頁面時,由nginx自己處理。當訪問jsp動態頁面時,nginx根據設置的權重將請求分流到35,38兩臺服務器中處理,處理后結果返回到頁面顯示。?
????????訪問localhost:8888/myspring/default.jsp,將會隨機分配給35或38的服務器處理。?
?????????
?????????
????說明:以上myspring是我使用的項目示例,項目直接路徑下包含test.html和default.jsp兩個文件。?
??????????nginx還有很多參數這里沒有使用到,具體可上Nginx官網了解。
轉載于:https://www.cnblogs.com/half-two-feet/p/4537773.html
總結
以上是生活随笔為你收集整理的使用nginx进行负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 开关按钮切换,类似于ip
- 下一篇: word2013 blog test