Nginx处理静态页面的效率明显高于tomcat,所以使用nginx处理用户请求的静态页面,tomcat处理动态页面,用nginx和tomcat达到动静分离的效果,从而提高并发性。

实现环境:redhat 6.5

实现主机: 172.25.16.2 nginx

                 172.25.16.3 tomcat

                 172.25.16.4 tomcat

软件下载:

                下载jdk : http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

                下载tomcat:http://tomcat.apache.org/download-80.cgi

                下载nginx:http://nginx.org/en/download.html

  1. 在 server2主机上源码编译 nginx

  [root@server2 nginx-1.6.2]# yum install -y pcre-devel gcc-c++ openssl-devel zlib-devel cmake                    //安装依赖包  

  [root@server2 ~]# tar -zxvf nginx-1.6.2.tar.gz

  [root@server2 ~]# cd nginx-1.6.2

  [root@server2 nginx-1.6.2]# vim auto/cc/gcc

    CFLAGS="$CFLAGS -g"     //去掉这行debug模式编译的注释,编译之后程序只有几百K
  [root@server2 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
  [root@server2 nginx-1.6.2]# make && make install
  [root@server2 sbin]# ln -s /usr/local/nginx/sbin/ /usr/local/bin/    //软连接nginx的启动文件

  [root@server2 ~]# useradd -d /usr/local/nginx/ -M -s /sbin/nologin nginx   //添加nginx用户并设置家目录

 2. 在server3和server4上配置tomcat 和 jdk

  [root@server3 ~]# tar -zxvf jdk-7u79-linux-x64.tar.gz
  [root@server3 ~]# mv jdk1.7.0_79/ /usr/local/jdk/

  [root@server3 ~]# vim /etc/profile

    JAVA_HOME=/usr/local/jdk
    PATH=$PATH:$JAVA_HOME/bin
    CLASSPATH=.:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
 [root@server3 ~]# source /etc/profile

 [root@server3 ~]# java -version     //显示版本,jdk配置成功
     java version "1.7.0_79"


 [root@server3 ~]# tar -zxvf apache-tomcat-8.0.24.tar.gz

 [root@server3 ~]# mv apache-tomcat-8.0.24/ /usr/local/tomcat/

 [root@server3 ~]# cd /usr/local/tomcat/bin/
 [root@server3 bin]# ./startup.sh      //开启tomcat

 [root@server3 bin]# netstat -antlp | grep java
  tcp        0      0 :::8080                     :::*                        LISTEN     1281/java    //8080 http端口:可以通过web页面直接访问       
  tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      1281/java  //8005 shutdown端口:主要负责启动关闭        
  tcp        0      0 :::8009                     :::*                        LISTEN      1281/java  //8009 ajp端口:主要负责通过ajp均衡        

//修改nginx主配置文件

[root@server2 ~]# vim /usr/local/nginx/conf/nginx.conf

user  nginx nginx;
worker_processes  2;

pid        logs/nginx.pid;
events {
        use epoll;
        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"';

   upstream westos {
        server 172.25.16.3:8080;
        server 172.25.16.4:8080;
        }

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        access_log logs/host.access.log;

    location ~ \.jsp$ {        //以.jsp结尾的动态页面交给代理westos处理即后端tomcat
        proxy_pass http://westos;
        }

    location ~ .*\.(html|jpg|txt)$ {     //由nginx处理静态页面
     expires 3d;
        }
    }

}
//现在就可以在客户端测试,当浏览器访问静态页面时nginx处理,访问jsp页面时后端tomcat处理;