Nginx+Tomcat动静分离
需求:nginx处理用户请求的静态页面,tomcat处理用户请求jsp页面,来实现动态分离,nginx处理静态页面效率远高于tomcat,这样一来就能更好的提高并发,处理性能。
准备软件:
下载jdk1.7:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
下载tomcat7.0:http://tomcat.apache.org/download-70.cgi
下载nginx1.6.3:http://nginx.org/en/download.html
1、JDK配置
[root@web1 ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)
[root@web1 ~]# uname -a
Linux web1 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@web1 ~]# cd tools
[root@web1 tools]# tar xf jdk-1.7-linux-32bit.tar.gz -C /usr/local/
[root@web1 tools]# mv /usr/local/jdk1.7.0_67 /usr/local/jdk
[root@web1 tools]# vi /etc/profile
JAVA_HOME=/usr/local/jdk
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export JAVA_HOME PATH CLASSPATH
[root@web1 tools]# source /etc/profile
[root@web1 tools]# java -version #显示版本说明成功
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode)
2、Tomcat配置
[root@web1 tools]# tar xf apache-tomcat-7.0.64.tar.gz -C /usr/local/
[root@web1 tools]# mv /usr/local/apache-tomcat-7.0.64 /usr/local/tomcat
#默认tomcat是root身份运行的,这样不安全,我们设置来用普通用户
[root@web1 tools]# groupadd tomcat
[root@web1 tools]# useradd -g tomcat tomcat
[root@web1 tools]# passwd tomcat
[root@web1 tools]# chown tomcat.tomcat -R /usr/local/tomcat
[root@web1 tools]# su - tomcat /usr/local/tomcat/bin/startup.sh
[root@web1 tools]# echo
"su - tomcat /usr/local/tomcat/bin/startup.sh"
>> /etc/rc.local #开机启动
[root@web1 tools]# netstat -tunlp|grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 14940/java
tcp 0 0 :::8009 :::* LISTEN 14940/java
tcp 0 0 :::8080 :::* LISTEN 14940/java
3、Nginx安装配置
安装过程详见:http://linuxzkq.blog.51cto.com/9379412/1630217
#主配置文件配置
[root@web1 ~]# grep -Ev "#|^$" /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location ~\.(html|htm)$ {
#jsp网站程序根目录,一般nginx与tomcat在同一个目录
root html;
index index.jsp index.do index.html index.htm;
}
location ~\.jsp$ {
index index.jsp;
proxy_pass http://127.0.0.1:8080; #来自jsp请求交给tomcat处理
proxy_redirect off;
proxy_set_header Host $host;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout
90
; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout
90
; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers
6
32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*
2
)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
location ~\.(png|js|css|jpg|gif|xml|svg|ico)$ { #由nginx处理静态页面
root html/ROOT;
expires 30d; #使用expires缓存模块,缓存到客户端
30
天
}
}
}