Nginx+Tomcat实现动静分离

一、Tomcat安装与配置

2.1、Tomcat概述
Tomcat是Apache 软件基 金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。目前最新版本是8.0。

2.2、安装jdk

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jdk-8u91-linux-x64.tar.gz
tar xf jdk-8u91-linux-x64.tar.gz -C ../source/
mv jdk1.8.0_05/ /usr/local/java
echo  -e  "JAVA_HOME=/usr/local/java\nJRE_HOME=/usr/local/java/jre\nPATH=$JAVA_HOME/bin:$PATH:$JRE_HOME/bin\nCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib\nexport JAVA_HOME JRE_HOME PATH CLASSPATH" >>/etc/profile
source /etc/profile
java –version

 

wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.0.M10/bin/apache-tomcat-9.0.0.M10.tar.gz  

 

2.3、安装Tomcat

http://apache.fayea.com/apache-mirror/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54.tar.gz
# tar -xvf apache-tomcat-7.0.54.tar.gz
# mv apache-tomcat-7.0.54 /usr/local/tomcat

2.4、配置Tomcat

# vim /usr/local/tomcat/conf/server.xml
    <Connector port="8080" protocol="HTTP/1.1"  索搜此项修改默认WEB端口
               connectionTimeout="20000"
               redirectPort="8443" />
<Host name="localhost"  appBase="zeng"  可以修改域名或者IP,但作代理请保持localhost
            unpackWARs="true" autoDeploy="true">
          <Context path="" docBase="/opt/zeng"></Context> 新增此项修改WEB的家目录

2.5、启动tomcat


[root@slave bin]# ./startup.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

2.6、tomcat测试

http://10.0.0.202:8080 在没有修改WEB目录的情况出现此页面就算成功

二、Nginx的安装与配置

4.1、安装依赖库

# yum -y install gcc openssl-devel zlib-devel

4.2、安装pcre-delvel库

①pcre-8.01.tar.gz
# cd /soft/
# tar xf pcre-8.01.tar.gz -C tmp/
# cd tmp/pcre-8.01/
#./configure && make && make install

4.3、安装libmd5

②libmd5-0.8.2b.tar.gz
# cd /soft/
#tar xf libmd5-0.8.2b.tar.gz -C tmp/

4.4、安装Nginx

③nginx-0.8.55.tar.gz
# cd /soft/
# tar nginx-0.8.55.tar.gz -C tmp/
# cd tmp/nginx-0.8.55/
#CONFOPTS="
--user=www \
--group=www \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-md5=/soft/md5/ \
--with-sha1=auto/lib/sha1 \
--with-pcre=/soft/pcre-8.01 \
--without-select_module \
--without-poll_module \
--without-http_ssi_module \
--without-http_userid_module \
--without-http_geo_module \
--without-http_map_module \
--without-http_memcached_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--prefix=/usr/local/services/nginx-0.8.55 \
"
#./configure $ CONFOPTS >/dev/null
# make >/dev/null && make install >/dev/null
#chown  user_00.users /data/www/pvp.fanhougame.net –R

编译参数详解:
--with-http_realip_module
此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用。

-with-http_stub_status_module
这个模块可以取得一些nginx的运行状态,

--with-http_gzip_static_module
这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩。

--with-md5=/soft/md5/
设定md5库文件路径

--with-sha1=auto/lib/sha1
设定sha1库文件路径
--with-pcre=/soft/pcre-8.01
设定PCRE库路径

--without-select_module
标准连接模式。默认情况下自动编译方式。您可以启用或禁用通过使用-select_module和不带- select_module配置参数这个模块

--without-poll_module
不使用poll模块

--without-http_ssi_module
不使用ngx_http_ssi_module模块,此模块处理服务器端包含文件(ssi)的处理.

--without-http_userid_module
不使用ngx_http_userid_module模块

--without-http_geo_module
这个模块基于客户端的IP地址创建一些ngx_http_geoip_module变量,并与MaxMindGeoIP文件进行匹配,该模块仅用于 0.7.63和0.8.6版本之后。但效果不太理想,对于城市的IP记录并不是特别准确,不过对于网站的来源访问区域的分析大致有一定参考性

--without-http_map_module
不使用ngx_http_map_module模块

--without-http_memcached_module
不使用ngx_http_memcached_module模块

--without-mail_pop3_module
不允许ngx_mail_pop3_module模块

--without-mail_imap_module
不允许ngx_mail_imap_module模块

--without-mail_smtp_module
不允许ngx_mail_smtp_module模块


4.4、配置说明


配置文件位置:/usr/local/nginx/conf/nginx.conf
1. Nginx配置文件分为4个部分
2. main(全局设置)
3. server(主机设置)
4. upstream(负载均衡设置)
5. localtion(URL匹配特定位置的设置)
这四个 server继承main  location继承server
upstream即不会继承
其它设置也不会被继承.

主配置文件Nginx.conf内容如下:
#==================================一全局配置#========================
user  user_00 users;  #这个模块指令,指Nginx Worker 运用的用户和组,默认为nobody
worker_processes  8;  #指定了要开启的进程数,每进程占用10M~12M的内存,建议和CPU的核心数量一样多的进程就行了。


error_log  logs/error.log; #全局错误日志
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;  
 
pid        logs/nginx.pid;  #:用来指定进程ID的存储位置.


#Specifies the value for maximum file descriptors that can be opened by this process.
#events 用来指定Nginx工作模式以及连接数上限
events {
    use epoll;  #使用epoll高效模式,适用于Linux,Unix使用kqueue
    worker_connections  100000; #定义Ningx没个进程最大的连接数。默认为1024,受到文件句柄的约束。
}
worker_rlimit_nofile 100000; #打开的文件句柄数量最高为10万

#==================================二、HTTP配置========================
http {
    include       mime.types;  #实现对配置文件所包含的文件设定
    default_type  application/octet-stream; #属于HTTP核心模块,默认设定为二进制流
    server_tokens off;   #禁止错误页面里显示nginx的版本号

         # 定义日志处理的格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

         # 定义它的hash表为128K
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,一般一个请求头的大小不会超过1k
    large_client_header_buffers 4 32k; #客户请求头缓冲大小 nginx默认会用client_header_buffer_size这个buffer来读取header值
    client_max_body_size 8m; #设定通过nginx上传文件的大小

         #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,
     #对于普通应用,必须设为on。
          #如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile          on;
    tcp_nopush        on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    tcp_nodelay       on;

    #keepalive_timeout  0;
    keepalive_timeout 60; #keepalive超时时间。连接保持活动时间超过这个,将被关闭掉

         #===================重要位置============
    fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间。
    fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
    fastcgi_read_timeout 300; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
    fastcgi_buffer_size 254k; #指定读取FastCGI应答第一部分需要用多大的缓冲区
    fastcgi_buffers 16 256k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
    fastcgi_busy_buffers_size 512k; #这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。
    fastcgi_temp_file_write_size 512k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。

    gzip              on; #该指令用于开启或关闭gzip模块(on/off)
    gzip_min_length   1k; #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取
    gzip_buffers      4 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
    gzip_http_version 1.0; #识别http的协议版本
    gzip_comp_level   2;   #gzip压缩比,1压缩比最小处理速度最快
         #匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的
    gzip_types        text/plain application/x-javascript text/css application/xml text/javascript;
    gzip_vary         on; #和http头有关系,加个vary头,给代理服务器用的

    charset      utf-8;  #字符集为utf-8

    access_log   off;    # 日常日志关闭
    log_not_found off;   # 日常日志关闭

        error_page  400 403 405 408  /40x.html;  # 错误返回页面
        error_page  500 502 503 504  /50x.html;  # 错误返回页面
         #===================Server虚拟机配置保持默认============
    server {
        listen       80 default;   #默认监听端口号为80
        server_name  _;
         return       444;
    }
#===================自定义虚拟机配置文件===========
include vhost/vhost.www.dali.com;
}

4.6、Nginx反向代理(负载)


Nginx+tomcat实现负载均衡和动静分离
Nginx反向代理 10.0.0.201:80
Tomcat WEB1: 10.0.0.202
Tomcat WEB1: 10.0.0.203
在Nginx.cnf增加如下:
include vhost/vhost.aatest.com;

配置反向代理目录
在conf/vhost/vhost.daili.com 编辑内容为;
upstream webcount {
      server  10.0.0.202:8080 weight=1 max_fails=3 fail_timeout=20s;
server  10.0.0.203:8080 weight=1 max_fails=3 fail_timeout=20s;

}
server { 
        listen       80; 
        server_name  www.aatest.com; 
        charset utf-8; 
            location ~ (\.jsp)|(\.do)$ {   #jsp和do的交给tomcat处理实现动静分离
            index  index.html index.htm index.jsp index.do; 
            proxy_pass        http://webcount; 
            proxy_set_header  X-Real-IP  $remote_addr; 
            client_max_body_size  100m; 
            }
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|html|htm)$ #html和图片的和其它的静态内容存路径html
    { 
       root   /opt/alvin;    #可自由指定
    expires      30d; 
    } 
}~ 

4.5、启动与平滑重启

# cd /usr/local/services/nginx-0.8.55/sbin/
# ./nginx –t 检测配置文件是否有错误
# ./nginx 启动nginx
# ./nginx -s reload

三、动静分离测试

3.1、在tomcat上

在WEB目录创建
# cd /opt/zeng
# echo “this is jsp” >index.jsp
# echo “this is do” >index.do
http://10.0.0.202:8080/index.jsp
http://10.0.0.202:8080/index.do

3.2、在Nginx上

在Nginx的/opt/alvin目录下
# cd /opt/alvin
# echo “this is index ” >index.html
# echo “this is index a” >aa.html

3.3、在浏览器里面

http://www.aatest.com/ 


 http://www.aatest.com/aa.html



http:// www.aatest.com /index.jsp

http://www.aatest.com/index.do


posted on 2016-08-29 21:06  losbyday  阅读(857)  评论(0编辑  收藏  举报

导航