参考:http://www.iyunv.com/thread-26870-1-1.html


实验环境:192.168.127.141 nginx+tomcat

          192.168.127.130  tomcat

安装nginx和tomcat省略


nginx配置文件:

user nobody nobody;
worker_processes  8;
error_log  /usr/local/nginx/logs/error.log crit;
pid        /usr/local/nginx/logs/nginx.pid;
events {
    use epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  /usr/local/nginx/logs/access.log;
    charset utf-8;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  60;
    client_body_buffer_size  512k;
    proxy_connect_timeout    5;
    proxy_read_timeout       60;
    proxy_send_timeout       5;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    upstream web_server {
      server 192.168.127.130:8080 weight=1;
      server 192.168.127.141:8080 weight=1;
    }
    server {
        listen       80;
        server_name  192.168.127.141;
        root   /usr/local/nginx/html;
        index  index.html index.htm index.php;
      location ~ \.php$ {
         include fastcgi_params;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
       
   ##动态文件转发到后端的tomcat集群
     location ~.*\.(php|jsp|cgi?$){
            proxy_pass http://web_server;
            proxy_set_header Host  $host;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP  $remote_addr;
        }
      location ~.*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
           # root /usr/local/nginx/html;  ##这个目录根据自己的情况定义,这是静态文件所放的目录
            expires 30d;
        }
     
     ## js、css文件从本地读取,且定义在浏览器中缓存1小时
       location ~ .*\.(js|css)?$
    
        {
            expires 1h;
        }
}
    }


注意:

把tomcat下面的ROOT目录复制到/usr/local/nginx/html这个目录下面

nginx配置文件设置:

root  /usr/local/nginx/html/ROOT;

测试反向代理和负载均衡:

这样我们直接访问http://192.168.127.141就出现了tomcat的页面了,说明我们的反向代理成功了,如图: 两个不同的页面说明我们的负载均衡也成功了。


wKioL1eRl9-CDJuEAAEftWED9PA971.png-wh_50

wKioL1eRl9-BY_A3AAEJvO3Spkg694.png-wh_50

测试动静分离:

分别在两台tomcat服务器上面建立两个文件,便于测试

cd /usr/local/tomcat//webapps/
 mkdir shop
 vi shop/test.html 
141  html !
 vi shop/test.jsp
141 jsp!


cd /usr/local/tomcat/webapps/
 mkdir shop
 vi shop/test.html 
130  html!
 vi shop/test.jsp
130 jsp!



测试tomcat是否正常工作,浏览器中访问,能够正常显示测试页面,表明工作正常

http://192.168.127.141:8080/shop/test.html

http://192.168.127.141:8080/shop/test.jsp

http://192.168.127.130:8080/shop/test.html

http://192.168.127.130:8080/shop/test.jsp



在nginx的/usr/local/nginx/html/ROOT     目录下创建测试目录shop、测试文件test.html和test.jsp

mkdir shop

vi shop/test.html

141 html nginx

vi shop/test.jsp

141jsp

 在浏览器中访问http://192.168.127.141/shop/test.html ,无论怎样刷新,页面都显示如下:


wKiom1eRmSWDnJDWAABCdtA0AHY589.png

在浏览器中访问http://192.168.127.141/shop/test.jsp,刷新几次,显示不同的内容,如下

wKioL1eRmT7wC5YRAABC1FRZu-A364.png

wKioL1eRmT7C1SEMAABDINbo9Ao853.png

  从结果来看,访问html静态文件时,返回的是nginx中的文件,而访问jsp动态页面时则是轮询后端的tomcat集群。至此,反向代理+动静分离已经实现。

最后我们来比较动静分离与单纯的反向代理的性能差异:

安装ab工具 yum install httpd-tools


测试tomcat

 ab -n 20000 -c 3000 http://127.0.0.1:8080/shop/test.html   
Server Software:        Apache-Coyote/1.1
Server Hostname:        127.0.0.1
Server Port:            8080
Document Path:          /shop/test.html
Document Length:        9 bytes
Concurrency Level:      3000
Time taken for tests:   7.115 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      5011750 bytes
HTML transferred:       180423 bytes
Requests per second:    2810.88 [#/sec] (mean)
Time per request:       1067.281 [ms] (mean)
Time per request:       0.356 [ms] (mean, across all concurrent requests)
Transfer rate:          687.86 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  637 903.3     85    3110
Processing:     3  119 270.8     74    4494
Waiting:        1   95 270.2     56    4492
Total:         25  756 1004.8    170    6766

一共请求20000次,每次3000的并发访问,总共用的时间为7.115秒,吞吐量为0.68M/s


测试nginx

ab -n 20000 -c 3000 http://127.0.0.1:80/shop/test.html     
Server Software:        nginx/1.6.2
Server Hostname:        127.0.0.1
Server Port:            80
Document Path:          /shop/test.html
Document Length:        15 bytes
Concurrency Level:      3000
Time taken for tests:   2.483 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      6960599 bytes
HTML transferred:       315435 bytes
Requests per second:    8056.09 [#/sec] (mean)
Time per request:       372.389 [ms] (mean)
Time per request:       0.124 [ms] (mean, across all concurrent requests)
Transfer rate:          2738.05 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  169 191.8    129    1133
Processing:    87  176  49.1    173     335
Waiting:        0  133  45.1    128     281
Total:        123  345 204.2    315    1400

相同压力下,访问nginx总共所用时间2.483秒,吞吐量位2.73M/s, 可见nginx在处理静态页面上远优于tomcat。