Chinaunix首页 | 论坛 | 博客
  • 博客访问: 638777
  • 博文数量: 110
  • 博客积分: 3808
  • 博客等级: 中校
  • 技术积分: 1930
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 14:32
个人简介

声的伟大!

文章分类

全部博文(110)

文章存档

2014年(1)

2013年(2)

2012年(12)

2011年(81)

2010年(14)

分类: LINUX

2012-01-07 18:30:59

此篇文章只为Nginx与Tomcat的结合,纯Java环境,一台服务器;如果是多台服务器需要配置session共享。

一、先安装Nginx:安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等
1.pcre的安装
# unzip pcre-8.21.zip
# ./configure
# make && make install

2.Nginx的安装
# groupadd www
# useradd -g www -s /sbin/nologin www
# mkdir /usr/local/webserver/nginx/logs -p
# tar xvf nginx-1.0.11.tar.gz
# cd nginx-1.0.11
# ./configure --prefix=/usr/local/webserver/nginx --sbin-path=/usr/sbin/nginx --conf-
path=/etc/nginx/nginx.conf --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
# make && make install


二、apache-tomcat的安装,而apache-tomcat又需要java环境的支持
1.JDK的安装
# chmod x jdk-6u29-linux-i586.bin
# ./jdk-6u29-linux-i586.bin
# mv jdk1.6.0_29 /usr/local/
到此JDK已经安装完成

2.到/usr/bin目录下,把原用的java,javac文件删除:
# rm -rf /usr/bin/java
# rm -rf /usr/bin/javac
在/usr/bin 下建立 软连接 java
# ln -sv /usr/local/jdk1.6.0_29/bin/java /usr/bin/java
# ln -sv /usr/local/jdk1.6.0_29/bin/javac /usr/bin/javac
# java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)
# javac -version
javac 1.6.0_29

3.为了方便下在的工作建立两个软链接:
# ln -sv /usr/local/jdk1.6.0_29 /usr/local/jdk
# ln -sv /usr/local/jdk1.6.0_29/jre /usr/local/jre

4.Tomcat的安装
# tar xvf apache-tomcat-6.0.33.tar.gz
# mv apache-tomcat-6.0.33 /usr/local/tomcat
设置环境变量:
# vim ~/.bash_profile //仅当前用户有效
#Set  Environment  by NetSeek
JAVA_HOME=/usr/local/jdk
export JAVA_HOME
JRE_HOME=/usr/local/jre
export JRE_HOME
CLASSPATH=/usr=/usr/local/tomcat/common/lib/:/usr/local/jdk/lib:/usr/local/jre/lib
export CLASSPATH
PATH=$PATH:/usr/local/tomcat/bin/:/usr/local/jdk/bin:/usr/local/jre/bin
export PATH
TOMCAT_HOME=/usr/local/tomcat
export TOMCAT_HOME
# source ~/.bash_profile

5.启动tomcat以检查是否存在错误:
# cd /usr/local/tomcat/bin/
# ./startup.sh
[url][/url]地址:8080 可以看到猫头:)成功了。


三、nginx的配置
1.# vim /etc/nginx/nginx.conf
user  www www;
worker_processes 8;
error_log  /usr/local/webserver/nginx/logs/web-error.log;
pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
    }

http
    {
        include       mime.types;
         default_type  application/octet-stream;
                #charset gb2312
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
                client_max_body_size     10m;
        large_client_header_buffers 4 32k;
        sendfile on;
        tcp_nopush     on;
        keepalive_timeout 60;
        tcp_nodelay on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;

        #limit_zone  crawler  $binary_remote_addr  10m;

                upstream  tomcat {
                # ip_hash;
                server 127.0.0.1:8080;
                }

server
    {
    listen       80;
    server_name web.test.com;
        index index.html index.htm index.jsp default.jsp index.do default.do;
        root /usr/local/tomcat/webapps/test/;

        if ($host = 'test.com' )
        {

                rewrite ^/(.*)$ permanent;  
       
        }
        if (-d $request_filename)
       
        {
   
                rewrite ^/(.*)([^/])$ permanent;
   
        }

        location ~ \.(jsp|jspx|do)?$
        {       
        proxy_redirect off;
        proxy_set_header Host  $host;
        proxy_set_header   X-Forwarded-For  $remote_addr;
        proxy_pass
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

        {

                expires 30d;

        }

        location ~ .*\.(js|css)?$

        {

                expires 1h;

        }
     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
     access_log  /usr/local/webserver/nginx/logs/web-access.log  access;

    }

}



2.在/usr/local/tomcat/webapps目录下新建test目录,在test目录下新建WEB-INF目录,在WEB-INF目录下新建web.xml文件,内容:
    xmlns:xsi=""
    xsi:schemaLocation=" /web-app_2_4.xsd"
    version="2.5">
       TomcatDemo
      


3.在test目录下新建test.jsp测试页,内容:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
Cluster App Test

Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>
<%
  out.println("
ID " + session.getId()+"
");

String dataName = request.getParameter("dataName");
  session.setAttribute("myname","session?");
  if (dataName != null && dataName.length() > 0) {
     String dataValue = request.getParameter("dataValue");
     session.setAttribute(dataName, dataValue);
  }
  out.print("Session P");
  Enumeration e = session.getAttributeNames();
  while (e.hasMoreElements()) {
     String name = (String)e.nextElement();
     String value = session.getAttribute(name).toString();
     out.println( name + " = " + value+"
");
         System.out.println( name + " = " + value);
   }
%>
 

    name:
    

    key:
    

   
  




4.启动nginx服务:service nginxd start

5.访问站点如下:


查看日志如下:


到这里,已基本完成

阅读(2518) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~