nginx 搭建及tomcat集群分冈、session共享(redis)

  • 環(huán)境
    centos6.5 nginx-1.8.1 tomcat7.0.75 redis2.8
  • nginx搭建
    1. 安裝nginx 依賴包
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
  1. 安裝nginx
\#tar  -zxvf   nginx-1.8.1.tar.gz     //解包 
\#mv  nginx-1.8.1   nginx                //進入目錄
\#cd  nginx                   //進入目錄
./configure --user=chenshb --group=chenshb --prefix=/data1/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-cc-opt='-O3' --with-cpu-opt=opteron --with-http_gzip_static_module
//面我們指定把nginx安裝到/data1/nginx目錄下并且安裝Nginx性能檢測模塊和SSL安全模塊以及FLV模塊支持 
make  &&  make  install                                  //編譯和安裝 
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local     //添加到開機自啟動
  • 目錄說明:4個文件目錄 conf-->配置文件 html--->程序目錄 logs--->日志目錄 sbin--->命令目錄
  • Nginx相關(guān)操作命令
    # /usr/local/nginx/sbin/nginx // 啟動nginx
    # kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` //停止nginx
    #kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //重啟nginx
    ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' 輸入此命令查看Nginx主進程號屏幕顯示的即為Nginx主進程號,例如: 6302
    這時偎血,執(zhí)行以下命令即可使修改過的Nginx配置文件生效:
    #kill -HUP 6302
  • 平滑重啟nginx:(比如你編輯了nginx.conf配置了文件诸衔,那么無需要重新啟動nginx
    執(zhí)行以下命令即可立即加載!)
    對于Nginx 0.8.x以上的版本颇玷,現(xiàn)在平滑重啟Nginx配置非常簡單笨农,執(zhí)行以下命令即可:
    #/usr/local/nginx/sbin/nginx -s reload
    #ps -aux | grep nginx
  1. 配置nginx (具體可參考nginx配置文件的詳解)
- 修改nginx.conf
# erdo
user             root;
worker_processes 4;
events {
  worker_connections 8192;
}
http {
  include           mime.types;
  default_type      application/octet-stream;
  server_tokens     off;
  sendfile          on;
  tcp_nopush        on;
  keepalive_timeout 65;
   client_max_body_size    200m;
    client_body_temp_path /usr/local/nginx/client_body_temp;
  fastcgi_buffers         4 64k;
  fastcgi_buffer_size     64k;
  fastcgi_send_timeout    300;
  fastcgi_read_timeout    300;
  fastcgi_connect_timeout 300;
  fastcgi_busy_buffers_size    128k;
  fastcgi_temp_file_write_size 128k;
  gzip              on;
  gzip_vary         on;
  gzip_proxied      any;
  gzip_disable      "msie6";
  gzip_min_length   1024;
  gzip_http_version 1.0;
  gzip_types        text/plain text/xml application/xml text/javascript text/css application/x-javascript application/javascript;
  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_set_header X-Forwarded-For2 $proxy_add_x_forwarded_for;
  #add_header       X-Frame-Options  "SAMEORIGIN";
  # deny ip/empty_server_name access
  server {
          listen       80;
          server_name  _;
          return       403;
 }
  include /usr/local/nginx/conf/vhost.d/*conf;
}
# vim:set et ts=2 sw=2: #
- 在目錄vhost.d下增加tomcatfedin.conf
upstream turn_server {
    server   localhost:80;
}
upstream tomcat {
    #ip_hash;
    server   192.168.172.102:8080 max_fails=3 weight=1 fail_timeout=60s;
    server   192.168.172.102:8081 max_fails=3 weight=1 fail_timeout=60s;
}
server {
    listen       80;
    server_name  192.168.172.102;
    server_name_in_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_set_header   X-Forwarded-Proto $scheme;
    access_log  /usr/local/nginx/logs/error_log;
    error_log   /usr/local/nginx/logs/error_log;
    root /usr/local/nginx/html;
    index index.php index.html;
    # issues1096
    if ($request_uri ~ " ") {
        return 404;
    }
    # issues529 940
    location ~* ^/(gtyc|web)/login\.php {
        return 404;
    }
    location ~* ^/tool/dologin\.php {
        return 404;
    }
    # issues529 940
#location ~* ^/ebook/(book_index|denglu|dinggou)\.php {
#   return 404;
#   }
    location ~* ^/ePubManager {
       proxy_pass        http://turn_server;
    }
#tomcat
   location ~* ^/tf/tm1 {
       proxy_pass http://localhost:8080;
}
   location ~* ^/tf/tm2 {
       proxy_pass http://localhost:8081;
}
   location ~* ^/docs {
       proxy_pass http://tomcat;
}
    location ~*  {
     #  proxy_pass        http://turn_server;
     proxy_pass       http://tomcat;
    }
#    include /usr/local/nginx/conf/vhost.d/comm;
}
  1. tomcat 安裝(具體可以參考網(wǎng)絡(luò)上)
    安裝兩個相同配置的tomcat8080 tomcat8081
    在tomcat ROOT下新建JSP文件
unzip apache-tomcat-6.0.44.zip
mv  apache-tomcat-6.0.44 tomcat6
  1. 啟動nginx tomcat與簡單測試
    修改ROOT 目錄下index.html 在各自的服務(wù)器上加入8080或8081;訪問http://192.168.172.102
    則輪替出現(xiàn)如下圖所示;表示已完成nginx+tomat集群的搭建


    8080.png

    8081.png
  2. tomcat集群間session的共享

  3. session 存在數(shù)據(jù)庫中(會加大數(shù)據(jù)庫的IO,增加數(shù)據(jù)庫的負擔(dān))

  4. session存在memcache或者redis中

  5. ngin中x的ip_hash帖渠,能夠?qū)⒛硞€IP的請求定向到同一臺后端;

upstream nginx.example.com  
    {   
             server 192.168.172.102:8081;   
             server 192.168.172.102:8080;  
             ip_hash;  
    }  
    server  
    {  
             listen 80;  
             location /  
             {  
                     proxy_pass  
                    http://nginx.example.com;  
             }  
 }  
  • session共享
  1. redis 配置 192.168.172.102:6379
  2. 兩個tomcat7配置: 在server.xml或context.xml中配置
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />  
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"  
         host="192.168.172.103"  
         port="6379"   
         database="0"   
         maxInactiveInterval="60"/> 

建議配置在context.xml 需在$tomcat/lib/下加增加JAR包:commons-pool-1.6.jar谒亦、jedis-2.1.0.jar、tomcat-redis-session-manager-1.2-tomcat-7.jar 下載地址空郊。

  1. tomcat 第二種配置 如果需要支持redis集群的配置時份招,可以參考來開源項目https://github.com/jcoleman/tomcat-redis-session-managers; tomcat配置:
 <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
     host="192.168.172.103"
     port="6379"
     database="0"
     maxInactiveInterval="60"/>

此配置及jar包下載參考https://github.com/izerui/tomcat-redis-session-manager;

  1. 測試session 共享示例
    在兩個tomcat7 下加入 test.jsp
<%@ page language="java" %>
<html>
  <head><title>Tomcat8081</title></head>
  <body>
    <h1><font color="red">Tomcat8081.test</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("test","test"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

測試效果:


tomcat_8080

tomcat_8081

出現(xiàn)以上效果說是有session共享成功

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市狞甚,隨后出現(xiàn)的幾起案子锁摔,更是在濱河造成了極大的恐慌,老刑警劉巖哼审,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谐腰,死亡現(xiàn)場離奇詭異,居然都是意外死亡棺蛛,警方通過查閱死者的電腦和手機怔蚌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來旁赊,“玉大人桦踊,你說我怎么就攤上這事≈粘” “怎么了籍胯?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵竟闪,是天一觀的道長。 經(jīng)常有香客問我杖狼,道長炼蛤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任蝶涩,我火速辦了婚禮理朋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘绿聘。我一直安慰自己嗽上,他們只是感情好,可當我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布熄攘。 她就那樣靜靜地躺著兽愤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挪圾。 梳的紋絲不亂的頭發(fā)上浅萧,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機與錄音哲思,去河邊找鬼洼畅。 笑死,一個胖子當著我的面吹牛也殖,可吹牛的內(nèi)容都是我干的土思。 我是一名探鬼主播务热,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼忆嗜,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了崎岂?” 一聲冷哼從身側(cè)響起捆毫,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冲甘,沒想到半個月后绩卤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡江醇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年濒憋,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片陶夜。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡凛驮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出条辟,到底是詐尸還是另有隱情黔夭,我是刑警寧澤宏胯,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站本姥,受9級特大地震影響肩袍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜婚惫,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一氛赐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧先舷,春花似錦鹰祸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至尔破,卻和暖如春街图,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背懒构。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工餐济, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人胆剧。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓絮姆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親秩霍。 傳聞我的和親對象是個殘疾皇子篙悯,可洞房花燭夜當晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容