目的:nginx处理用户请求的静态页面,tomcat处理用户请求jsp页面,来实现动态分离,
nginx处理静态页面效率远高于tomcat,这样一来就能更好的提高并发,处理性能。
软件清单
软件名称 | 下载地址 |
jdk1.8 | http://download.oracle.com/otn-pub/java/jdk/8u73-b02/jdk-8u73-linux-x64.tar.gz |
tomcat7 | http://apache.fayea.com/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz |
nginx1.8 | http://nginx.org/download/nginx-1.8.1.tar.gz |
1. jdk和tomcat
## jdk和tomcat自动安装脚本 ## Email: caishui97@yahoo.com ## Time: 2016.05.08 ## 参考 http://stackoverflow.com/questions/10268583/how-to-automate-download-and-installation-of-java-jdk-on-linux check_ok() { if [ $? != 0 ] then echo "Error, Check the error ." exit 1 fi } if ! rpm -qa|grep -q "^wget-" then yum install -y wget check_ok else echo "wget is already installed." fi ## 下载解压jdk tar.gz cd /usr/local/src echo "Began download jdk..." wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u73-b02/jdk-8u73-linux-x64.tar.gz check_ok echo "Began uncompress tar.gz..." tar -zxf jdk-8u73-linux-x64.tar.gz check_ok echo "uncompress complated..." mv jdk1.8.0_73 /usr/local/jdk1.8.0_73 ## 也可以下载RPM ## wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u73-b02/jdk-8u73-linux-x64.rpm ## rpm -ivh jdk-8u73-linux-x64.rpm ## set java enviroment echo "set java enviroment" cat > /etc/profile.d/java.sh<<EOF JAVA_HOME=/usr/local/jdk1.8.0_73 JAVA_BIN=/usr/local/jdk1.8.0_73/bin JRE_HOME=/usr/local/jdk1.8.0_73/jre PATH=$PATH:/usr/local/jdk1.8.0_73/bin:/usr/local/jdk1.8.0_73/jre/bin CLASSPATH=/usr/local/jdk1.8.0_73/JRE/LIB:/usr/local/jdk1.8.0_73/lib:/usr/local/jdk1.8.0_73/jre/lib/charsets.jar EOF check_ok source /etc/profile.d/java.sh java -version ## tomcat安装 echo "Began download tomcat..." wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz echo "Began uncompress tar.gz..." tar -zxf apache-tomcat-7.0.69.tar.gz check_ok echo "uncompress complated..." mv apache-tomcat-7.0.69 /usr/local/tomcat cp /usr/local/tomcat/bin/catalina.sh /etc/init.d/tomcat check_ok echo "修改tomcat启动脚本" sed -i ‘2 i# chkconfig: 2345 63 37‘ /etc/init.d/tomcat check_ok sed -i ‘3 i# description: tomcat server init script‘ /etc/init.d/tomcat check_ok sed -i ‘4 iJAVA_HOME=/usr/local/jdk1.8.0_73‘ /etc/init.d/tomcat check_ok sed -i ‘5 iCATALINA_HOME=/usr/local/tomcat‘ /etc/init.d/tomcat check_ok echo "tomcat启动脚本修改成功" chmod 755 /etc/init.d/tomcat chkconfig --add tomcat chkconfig tomcat on service tomcat start ps aux|grep tomcat
2. Nginx源码安装配置
groupadd nginx useradd -g nginx -s /sbin/nologin nginx yum install –y make zlib-devel openssl-devel pcre-devel tar zxvf nginx-1.4.4.tar.gz cd nginx-1.4.4 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module make && make install
3. Nginx主配置文件(/usr/local/nginx/conf/nginx.conf
)
user nginx; worker_processes 1; error_log logs/error.log; 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"‘; access_log logs/access.log main; sendfile on; keepalive_timeout 65; #gzip压缩功能设置 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-javascripttext/css application/xml; gzip_vary on; server { listen 80; server_name www.test.com; location / { #jsp网站程序根目录,一般nginx与tomcat在同一个目录 root /usr/local/tomcat/webapps/ROOT; index index.html index.jsp index.html; } 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 ~ .*\.(gif|jpg|png|bmp|swf)$ #由nginx处理静态页面 { expires 30d; #使用expires缓存模块,缓存到客户端30天 } location ~ .*\.(jsp|js|css)?$ { expires 1d; } error_page 404 /404.html; #错误页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
4. 编写nginx启动、停止、重启等SysV管理脚本,方便使用(/etc/init.d/nginx
)
#!/bin/bash # chkconfig: 345 99 20 # description: Nginx servicecontrol script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG echo "Nginx servicestart success." ;; stop) kill -s QUIT $(cat $PIDF) echo "Nginx service stopsuccess." ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) echo"reload Nginx configsuccess." ;; *) echo "Usage: $0{start|stop|restart|reload}" exit 1 esac
[root@localhost ~]# chmod +x /etc/init.d/nginx [root@localhost ~]# service nginx restart [root@localhost ~]# chkconfig --add nginx [root@localhost ~]# chkconfig nginx on
5,性能测试
下面我们使用的ab压力测试工具,模拟发起一次1万的并发请求,使用的index.html页面是百度首页代码
Nginx测试
Tomcat测试
主要参数说明:
Requests per second:平均每秒处理事务数
Time per request:平均事务响应时间
Tranfer rate:平均每秒网络吞吐量
经上面测试得出:nginx每秒处理请求6000次,而tomcat每秒只处理请求1000次。
由此看来,nginx是tomcat6倍的处理能力,如果网站程序静态页面多的话,就应该考虑使用Nginx与Tomcat整合来使用。
原文地址:http://9655582.blog.51cto.com/9645582/1794651