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

  • 環境
    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相關操作命令
    # /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
    這時,執行以下命令即可使修改過的Nginx配置文件生效:
    #kill -HUP 6302
  • 平滑重啟nginx:(比如你編輯了nginx.conf配置了文件,那么無需要重新啟動nginx
    執行以下命令即可立即加載!)
    對于Nginx 0.8.x以上的版本,現在平滑重啟Nginx配置非常簡單,執行以下命令即可:
    #/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 安裝(具體可以參考網絡上)
    安裝兩個相同配置的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 在各自的服務器上加入8080或8081;訪問http://192.168.172.102
    則輪替出現如下圖所示;表示已完成nginx+tomat集群的搭建


    8080.png

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

  3. session 存在數據庫中(會加大數據庫的IO,增加數據庫的負擔)

  4. session存在memcache或者redis中

  5. ngin中x的ip_hash,能夠將某個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

出現以上效果說是有session共享成功

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,797評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,179評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,628評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,642評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,444評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,948評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,040評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,185評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,717評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,794評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,418評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,414評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,750評論 2 370

推薦閱讀更多精彩內容