Windows+Nginx+Tomcat搭建負載平衡和叢集環境同時實現session共用(一)

來源:互聯網
上載者:User
摘要:隨著網站的訪問量越來越多,所以就考慮給網站增加伺服器了,現在比較流行的做法就是給網站做叢集環境,下面我把我做的過程記錄一下,方便日後查看,同時也希望可以協助到有需要的朋友。

一:首先是環境:

1.jdk 1.6.0_45

2.tomcat 6.0.44

3.nginx 1.8.0


二:jdk,tomcat,nginx的安裝:

1.jdk的安裝請參考:點擊開啟連結

2.nginx的安裝步驟

(1).首先下載nginx的windows版本,我這裡下載的nginx 1.8.0版本,下載地址:點擊開啟連結

(2).下載完成後,直接解壓到一個沒有中文的路徑下,我這裡是解壓到:D:\server\nginx-1.8.0

(3).通過dos命令進入nginx-1.8.0目錄下啟動nginx即可

(4)Windows下操作Nginx命令

1.啟動 nginx.exe start nginx 2.停止  nginx -s stop nginx -s quit

stop表示立即停止nginx,不儲存相關資訊

quit表示正常退出nginx,並儲存相關資訊 3.重啟 nginx -s reload 重啟(因為改變了配置,需要重啟)

3.tomcat的安裝(直接到tomcat的官網下載解壓縮版的即可)


三:把下載下載的tomcat分別複製3個,一共是個,其中三個做qdksDemo的叢集環境用,另外三個做qdkyDemo的叢集環境用,具體截圖如下:


複製完成後,要分別修改每個tomcat的連接埠和session共用的配置,具體的配置如下:

1.第一處要修改的連接埠號碼

<Server port="8005" shutdown="SHUTDOWN">
2.第二處要修改的連接埠號碼

<Connector port="8081" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" />
3.第三處要修改的連接埠號碼

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
4.第四處要增加的session共用配置,這個可以查看tomcat的官方文檔,裡面有配置直接複製到server.xml中就可以,地址: http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"                 channelSendOptions="6">          <Manager className="org.apache.catalina.ha.session.BackupManager"                   expireSessionsOnShutdown="false"                   notifyListenersOnReplication="true"                   mapSendOptions="6"/>          <!--          <Manager className="org.apache.catalina.ha.session.DeltaManager"                   expireSessionsOnShutdown="false"                   notifyListenersOnReplication="true"/>          -->          <Channel className="org.apache.catalina.tribes.group.GroupChannel">            <Membership className="org.apache.catalina.tribes.membership.McastService"                        address="228.0.0.4"                        port="45564"                        frequency="500"                        dropTime="3000"/>            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"                      address="auto"                      port="5000"                      selectorTimeout="100"                      maxThreads="6"/>            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>            </Sender>            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>          </Channel>          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"                 filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"                    tempDir="/tmp/war-temp/"                    deployDir="/tmp/war-deploy/"                    watchDir="/tmp/war-listen/"                    watchEnabled="false"/>          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>        </Cluster>

這樣一個tomcat的設定檔就修改完成了,其他5個tomcat的配置和這個流程一樣,只是對於的連接埠不能一樣


四:nginx的配置:

1.首先在nginx的目錄下建立一個新的檔案夾來存放不同項目之間的叢集設定檔,如下圖:


2.在manyvhost檔案夾裡面建立一個設定檔,分別對於兩個項目的叢集配置,如下圖:


3.qdks.conf和qdky.conf的配置內容如下:

(1).qdks.conf

upstream  qdks  {  server   localhost:8081 weight=1;  server   localhost:8082 weight=1;server   localhost:8083 weight=1;}server {listen       8086;server_name 192.168.1.103;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}root /data/projects/ycp/bill;# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"#    location ~ ^/static/assets/(.*)$#    {#alias /data/projects/payment/web/public/assets/$1;#      access_log off;#      #expires 3d;#    }location / {index  index.html index.htm  index.jsp;}location ~ .* {# proxy_pass_header Server; proxy_set_header Host $http_host;# proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header Pragma "no-cache"; proxy_pass http://qdks;}rewrite ^/admin/?$ /admin/login redirect;# for rewriterewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;#redirect to mobile wap#rewrite ^$ /m redirect;#rewrite ^/$ /mobile/user redirect;}

(2).qdky.conf

upstream  qdky  {     server localhost:7081 weight=1;     server localhost:7082 weight=1;   server localhost:7083 weight=1;}server {listen       7086;server_name  192.168.1.103;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}root /data/projects/ycp/bill;# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"#    location ~ ^/static/assets/(.*)$#    {#alias /data/projects/payment/web/public/assets/$1;#      access_log off;#      #expires 3d;#    }location / {index  index.html index.htm  index.jsp;}location ~ .* {# proxy_pass_header Server; proxy_set_header Host $http_host;# proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header Pragma "no-cache"; proxy_pass http://qdky;}rewrite ^/admin/?$ /admin/login redirect;# for rewriterewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;#redirect to mobile wap#rewrite ^$ /m redirect;#rewrite ^/$ /mobile/user redirect;}

這兩個設定檔就是我當前環境下的設定檔,其中qdks的項目的連接埠號碼8086,下面對應三個tomcat的連接埠為:8081,8082,8083;qdky的項目的連接埠號碼7086,下面對應三個tomcat的連接埠號碼為:7081,7082,7083


4.最後在nginx的conf目錄下的nginx.conf核心設定檔中引入上面的兩個項目的叢集配置,如下:

#user  nobody;worker_processes  1;error_log  logs/error.log;error_log  logs/error.log  notice;error_log  logs/error.log  info;pid        logs/nginx.pid;events {    worker_connections  5000;}http {    include       mime.types;    default_type  application/octet-stream;    #隱藏nginx的版本號碼    server_tokens off;    #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  logs/access.log  main;    #access日誌存放路徑和格式    access_log  logs/access.log;   #提升檔案傳輸效能   sendfile        on;   #tcp_nopush     on;   #keepalive_timeout  0;   #keepalive_timeout  65;      #設定用戶端能夠請求的單個檔案大小   client_max_body_size 300m;   keepalive_timeout  75;    #proxy參數    #跟後端伺服器串連的逾時時間,發起握手等候響應逾時時間    proxy_connect_timeout 5;    #串連成功後,等候後端伺服器的回應時間    proxy_read_timeout 600;    #後端伺服器資料回傳時間    proxy_send_timeout 600;    #代理請求緩衝區    proxy_buffer_size 16k;    #同上,告訴nginx儲存單個用的幾個buffer、最大用多空間    proxy_buffers 4 64k;    #如果系統很忙時可以申請更大的proxy_buffers,官方推薦*2    proxy_busy_buffers_size 128k;    proxy_temp_file_write_size 128k;    #gzip  on;    #開啟壓縮功能    gzip on;    gzip_min_length 1k;    gzip_buffers 4 16k;    gzip_http_version 1.1;     gzip_comp_level 2;    #壓縮層級從低到高1-9    gzip_types text/plain application/x-javascript text/css application/xml;    gzip_vary on;    #qdksDemo和qdkyDemo項目的虛擬目錄(用絕對路徑表示)    include  D:/server/nginx-1.8.0/manyvhost/qdks.conf;    include  D:/server/nginx-1.8.0/manyvhost/qdky.conf;    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       80;    #    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  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers   on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

五:分別部署對應的項目,來測試我們上面配置的叢集環境是否正常

1.下載對應的項目原始碼,下載後解壓到你的本地workspace下面,我的路徑是:D:\workspace_qdexam\qdksDemo和D:\workspace_qdexam\qdkyDemo

qdksDemo的下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187453

qdkyDemo的下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187455


2.分別部署到對應的tomcat下面,這裡我採用的是直接映射路徑的方法

(1).qdksDemo的部署方法是,在對應的三個tomcat的conf\Catalina\localhost目錄下建立一個設定檔qdksDemo.xml,該設定檔的內容如下:

<Context    path="/qdksDemo"   docBase="D:\workspace_qdexam\qdksDemo\WebContent"     debug="0"    privileged="true">      </Context>

(2).qdkyDemo的部署方法是,在對應的三個tomat的conf\Catalina\localhost目錄下建立一個設定檔qdkyDemo.xml,該設定檔的內容如下:

<Context path="/qdkyDemo"   docBase="D:\workspace_qdexam\qdkyDemo\WebContent" debug="0" privileged="true"></Context>


3.分別啟動這6個tomcat,如下圖:


4.啟動nginx,如下圖:


5.最後通過瀏覽器訪問這兩個項目,如下圖:



分別點擊重新整理,你會發現上面的實際訪問連接埠會變,但是下面的Session ID是不變的,這就說明基於nginx和tocmat的叢集搭建成功了。


最後我把我的tomcat和nginx都打包上傳,供大家參考。

tomcat的配置壓縮包下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187521

nginx的配置壓縮包下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187527




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.