标签:blog http io ar os 使用 sp for java
一: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.1572.在此主机上安装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.gzwget 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.gzshell> cd nginx-1.4.5shell> ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --user=apache --group=apache --with-http_ssl_module --with-http_gzip_static_moduleshell> make && make install2.启动nginxshell> /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf##添加nginx支持php3.首先为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.rpmshell> 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/profileshell> 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/tomcat1shell> cp apache-tomcat-7.0.52 /usr/local/tomcat2shell> chown apache.apache /usr/local/tomcat1shell> chown apache.apache /usr/local/tomcat2【tomcat1 and tomcat2】2.制作tomcat启动脚本shell> cp catalina.sh /etc/init.d/tomcat1shell> chmod +x /etc/init.d/tomcat1 shell> vim /etc/init.d/tomcat1shell> chkconfig --add tomcat1【tomcat1 and tomcat2】3.tomcat配置用户管理(可以不设置)shell>cd /usr/local/tomcat1/confshell> 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.重启tomcatshell> /etc/init.d/tomcat stopshell> /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之间切换
标签:blog http io ar os 使用 sp for java
原文地址:http://www.cnblogs.com/steven9801/p/4154978.html