nginx+tomcat+memcached

                                           nginx+tomcat(整合+负载均衡)+memcached

一.ngiux+tomcat整合

1.安装jdk (搭建jave包的环境)
下载 jdk-6u26-linux-x64.bin
sh jdk-6u26-linux-x64.bin
mv jdk1.6.0_26/ /usr/local/jdk

vim /etc/profile
nset pathmunge
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin

source /etc/profile


2安装tomcat
下载tomcat
tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local/tomcat

/usr/local/tomcat/bin/start.sh(启动tmcat)
netstat -ntal |grep 8080       (查看端口)
tcp        0      0 :::8080                     :::*                        LISTEN     

在浏览器里面输入自己的ip+:8080 就能看到tmcat家的小猫


3 安装nginx
yum install pcre-devel openssl-devel gcc zlib-devel -y
下载nginux
tar zxf nginx-1.0.2.tar.gz
cd nginx-1.0.2
vi auto/cc/gcc  (174-175行)
 # debug
        #CFLAGS=”$CFLAGS -g”    (注释掉这行,去掉debug模式编译,编译以后程序只有几百k)

vi src/core/nginx.h
#define NGINX_VERSION "1.0.6"  (11-13行)
        #define NGINX_VER "nginx" (修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译
后外界无法获取程序的版本号)

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
vi /usr/local/nginx/conf/nginx.conf
  user nginx nginx; #使用的用户和组
    worker_processes 8; #指定工作衍生进程数
    error_log logs/error.log info; #错误日志定义类型
    pid    logs/nginx.pid; #指定 pid 存放的路径

    events {
    use epoll; #使用高效网络I/O模型 具体内容查看 http:/wiki.codemongers.com/事件模型
    worker_connections 1024; #允许的连接数
    }
    http {
    include    mime.types; #文件扩展名与文件类型映射表
    default_type application/octet-stream; #默认文件类型
    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;
    server_names_hash_bucket_size 128; #服务器名字的hash表大小
    client_header_buffer_size 32k; #上传文件大小限制
    large_client_header_buffers 4 32k; #设定请求缓
    client_max_body_size 8m; #设定请求缓

    sendfile    on; 开启高效文件传输模式  
    tcp_nopush    on; 防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 65; 超时时间
    gzip on;
     gzip_min_length 1k; #最小压缩文件大小
     gzip_buffers 4 16k; #压缩缓冲区
     gzip_http_version 1.0; #压缩版本(默认1.1,前端为squid2.5使用1.0)
     gzip_comp_level 2; #压缩等级
     gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型
     gzip_vary on;

    server {
        listen    80;
        server_name localhost;

        location / {
            root html;
            index index.html index.htm;
        }

        location /status { #设定查看Nginx状态的地址
            stub_status on;
            access_log off;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }



nginx -t 查看编译的状态
nginx -s reload  从新启动服务


3整合
cd /usr/local/tomcat/webapps/ROOT/
vi test.jsp
The time is:<%=   location / {
 new java.util.Date() %>

vi /usr/local/nginx/conf/nginx.conf
user  www www;                    1-2行
worker_processes  8;

location / {                      48-59行
            root   html;
            index  index.html index.htm;
        }

        location /status {
         stub_status on;
         access_log off;
        }
       location ~\.jsp$ {
        proxy_pass http://192.168.0.1:8080;
}
}
}


   

这样你输入ip地址加测试文件,就能用nginx看到动态的测试东东了

 

二 nginx+tomcat负载均衡

修改配置文件
vi /usr/local/nginx/conf/nginx.conf
http {                                             17-22
   upstream        tomcat  {
   server 192.168.0.1:8080;
   server 192.168.0.2:8080;
}
   
default_type  application/octet-stream;           82-86
       location ~\.jsp$ {
        proxy_pass http://tomcat;
}
}
}



三.nginx+tomcat负载均衡同一ip地址访问同一机器
1.下载软件nginx-sticky-module-1.0.tar.gz
2.进入nginx-1.02从新编译
cd  cd nginx-1.0.2
./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-sticky-module-1.0(软件自己的绝对路径)
3.停止nginx
nginx -s stop
4.make && mak install
5.vim /usr/local/nginx/conf/nginx.conf
http {
   upstream        tomcat  {
   sticky;(19行体添加)
   server 192.168.0.1:8080;
   server 192.168.0.2:8080;
}

 

四.nginx+tomcat+memcached系统环境:rhel6 x64 selinux and iptables disabled
主机角色:node1: 192.168.0.91:nginx tomcat memcached
node2: 192.168.0.92:tomcat memcached
软件下载:http://www.nginx+tomcat+memcachednginx.org
http://code.google.com/p/memcached-session-manager/


1.下载软件
Kryo,此实验我们采用 kryo 方式。

2.将其移动到 cd /usr/local/tomcat/lib

3. 关闭nginux

4.安装 memcached
yum install memcached -y

5.启动memcached
/etc/init.d/memcached start

6.vi /usr/local/tomcat/conf/context.xml
复制
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
 ...
  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:localhost:11211,n2:localhost:11212"
    failoverNodes="n1"
    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
    />
修改为(最后行)
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:192.168.0.1:11211,n2:192.168.0.2:11211"
            failoverNodes="n1"

7.启动nginux

8.vi /usr/local/tomct/webapps/Root/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
 

本文出自 “罗宁的技术生活” 博客,谢绝转载!

你可能感兴趣的:(java,nginx,包,下载,local)