首页 > 代码库 > Nginx加多个tomcat实现负载均衡,动静分离
Nginx加多个tomcat实现负载均衡,动静分离
一:Nginx+Tomcat的动静分离
所谓动静分离就是通过nginx(或apache等)来处理用户端请求的图片、html等静态的文件,tomcat(或weblogic)处理jsp、do等动态文件,从而达到动静页面访问时通过不同的容器来处理。
nginx处理静态页面效率远高于tomcat,而tomcat擅长动态页面处理,这样一来就能更好的提高并发,处理性能。
页面压力测试
ab -n 1000 -c 200 http://10.10.54.157/index.html
二:具体步骤
//环境介绍
?
1 2 3 4 5 6 7 8 9 10 11 | 1.主机 centos6.4 IP:10.10.54.157 2.在此主机上安装nginx,及两个tomcat,nginx的反向代理功能可以把用户请求负载到tomcat上 nginx使用80端口,两个tomcat分别使用8080,9080端口 目的:当访问10.10.54.157这台服务器时,静态页面由nginx处理,动态页面由tomcat处理 3.软件下载 wget http: //nginx .org /download/nginx-1 .4.5. tar .gz wget http: //download .oracle.com /otn-pub/java/jdk/7u51-b13/jdk-7u51-linux-x64 .rpm #jdk提供tomcat运行环境 wget http: //apache .dataguru.cn /tomcat/tomcat-7/v7 .0.52 /bin/apache-tomcat-7 .0.52. tar .gz |
//安装nginx,并配置nginx支持php
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #nginx使用fpm的方式调用php #php5.4以后版本中已经支持php-fpm,所以只需要在php编译参数里指定--enable-fpm即可 1.编译安装 shell> yum -y install zlib-devel pcre-devel openssl-devel #nginx依赖包 shell> tar xvf nginx-1.4.5. tar .gz shell> cd nginx-1.4.5 shell> . /configure --prefix= /usr/local/nginx --with-pcre --with-http_stub_status_module --user=apache --group=apache --with-http_ssl_module --with-http_gzip_static_module shell> make && make install 2.启动nginx shell> /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx .conf ##添加nginx支持php 3.首先为php创建配置文件 shell> cp php.ini-production /usr/local/php/php .ini shell> mv /usr/local/php/etc/php-fpm .conf.default /usr/local/php/etc/php-fpm .conf shell> ln -s /usr/local/php/bin/php /usr/bin/ 4.配置php-fpm.conf shell> vi /usr/local/php/etc/php-fpm .conf -------------------------------- listen = /var/run/php-fpm .sock # 使用unix socket -------------------------------- 5.启动php-fpm shell> /usr/local/php/sbin/php-fpm |
//安装JDK
?
1 2 3 4 5 6 7 8 9 10 11 | shell> rpm -ivh jdk-7u51-linux-x64.rpm shell> vi /etc/profile ----------------------- export JAVA_HOME= "/usr/java/jdk1.7.0_51" export CLASSPATH= "$JAVA_HOME/lib:$JAVA_HOME/jre/lib" export PATH= "$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$HOME/bin" CATALINA_HOME= "/usr/local/tomcat" ----------------------- shell> source /etc/profile shell> java -version #显示版本说明成功 java version "1.7.0_45" |
//安装多个tomcat
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1.解压 shell> tar xvf apache-tomcat-7.0.52. tar .gz shell> cp apache-tomcat-7.0.52 /usr/local/tomcat1 shell> cp apache-tomcat-7.0.52 /usr/local/tomcat2 shell> chown apache.apache /usr/local/tomcat1 shell> chown apache.apache /usr/local/tomcat2 【tomcat1 and tomcat2】 2.制作tomcat启动脚本 shell> cp catalina.sh /etc/init .d /tomcat1 shell> chmod +x /etc/init .d /tomcat1 shell> vim /etc/init .d /tomcat1 shell> chkconfig --add tomcat1 【tomcat1 and tomcat2】 3.tomcat配置用户管理(可以不设置) shell> cd /usr/local/tomcat1/conf shell> vim tomcat- users .xml ----------------------------------------- <role rolename= "manager-gui" /> <role rolename= "admin-gui" /> <user username= "tomcat" password= "tomcat" roles= "admin-gui,manager-gui" /> #用户名和密码都为tomcat ----------------------------------------- 【tomcat1 and tomcat2】 4.重启tomcat shell> /etc/init .d /tomcat stop shell> /etc/init .d /tomcat start |
//配置nginx.conf实现负载均衡,动静分离
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | shell> vim /usr/local/nginx/conf/nginx .conf -------------------------------------------------------- user apache apache; #用户,tomcat默认首页也要改为apache用户 worker_processes 2; #error_log logs/error_nginx.log; pid logs /nginx .pid; events { worker_connections 1024; } http { include mime.types; default_type application /octet-stream ; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; keepalive_timeout 60; gzip on; #负载均衡 upstream www_zijian_com { server localhost:8080 max_fails=3 weight=1 fail_timeout=60s; server localhost:9080 max_fails=3 weight=1 fail_timeout=60s; } server { listen 80; server_name www.zijian.com; index index.jsp index.php index. do index.jsp; access_log logs /www .zijian.com_access_nginx.log; error_log logs /www .zijian.com_error.log; root /var/www/nginx ; #nginx默认根目录 location ~ \.(php|php5)$ { #支持php fastcgi_pass unix: /var/run/php-fpm .sock; #php-fpm.sock模块 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; include fastcgi.conf; } location ~ \.(jsp| do )$ { #jsp,do等页面交由tomcat处理 proxy_pass http: //www_zijian_com ; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } } } |
//测试
1.浏览器访问10.10.54.157 默认显示nginx根目录下的index.php页面
2.浏览器访问10.10.54.157/index.jsp 浏览器跳转到tomcat默认首页,在首页点击Server Status页面,并刷新浏览器,可以看到页面在两个tomcat之间切换
Nginx加多个tomcat实现负载均衡,动静分离
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。