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                //進(jìn)入目錄
\#cd  nginx                   //進(jìn)入目錄
./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性能檢測(cè)模塊和SSL安全模塊以及FLV模塊支持 
make  &&  make  install                                  //編譯和安裝 
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local     //添加到開機(jī)自啟動(dòng)
  • 目錄說明:4個(gè)文件目錄 conf-->配置文件 html--->程序目錄 logs--->日志目錄 sbin--->命令目錄
  • Nginx相關(guān)操作命令
    # /usr/local/nginx/sbin/nginx // 啟動(dòng)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主進(jìn)程號(hào)屏幕顯示的即為Nginx主進(jìn)程號(hào),例如: 6302
    這時(shí),執(zhí)行以下命令即可使修改過的Nginx配置文件生效:
    #kill -HUP 6302
  • 平滑重啟nginx:(比如你編輯了nginx.conf配置了文件,那么無需要重新啟動(dòng)nginx
    執(zhí)行以下命令即可立即加載!)
    對(duì)于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ò)上)
    安裝兩個(gè)相同配置的tomcat8080 tomcat8081
    在tomcat ROOT下新建JSP文件
unzip apache-tomcat-6.0.44.zip
mv  apache-tomcat-6.0.44 tomcat6
  1. 啟動(dòng)nginx tomcat與簡單測(cè)試
    修改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ù)庫中(會(huì)加大數(shù)據(jù)庫的IO,增加數(shù)據(jù)庫的負(fù)擔(dān))

  4. session存在memcache或者redis中

  5. ngin中x的ip_hash,能夠?qū)⒛硞€(gè)IP的請(qǐng)求定向到同一臺(tái)后端;

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. 兩個(gè)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集群的配置時(shí),可以參考來開源項(xiàng)目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. 測(cè)試session 共享示例
    在兩個(gè)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>

測(cè)試效果:


tomcat_8080

tomcat_8081

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,583評(píng)論 1 41
  • nginx在工作中已經(jīng)有好幾個(gè)環(huán)境在使用了,每次都是重新去網(wǎng)上扒博客,各種編譯配置,今天自己也整理一份安裝文檔和n...
    AndyChin閱讀 2,329評(píng)論 0 4
  • 1.簡介: ? Nginx:engine X ,2002年,開源,商業(yè)版? http協(xié)議:web服務(wù)器(類似于ht...
    尛尛大尹閱讀 1,896評(píng)論 0 3
  • 讀了申荷永教授的著作:《榮格與分析心理學(xué)》,想來寫點(diǎn)有關(guān)這本書的什么東西。寫了兩遍,每一遍都是半途而廢。原因是心理...
    瑪雅之釋閱讀 872評(píng)論 0 6
  • 抱怨會(huì)阻礙我前進(jìn)的腳步,以后決不再抱怨,找出自己的問題,承認(rèn)自己在很多時(shí)候表現(xiàn)出的尷尬。 抱怨會(huì)讓我產(chǎn)生很多負(fù)能量...
    雁過無痕有晴天閱讀 171評(píng)論 0 0