參考:http://www.iyunv.com/thread-26870-1-1.htmljavascript
實驗環境:192.168.127.141 nginx+tomcatphp
192.168.127.130 tomcatcss
安裝nginx和tomcat省略html
nginx配置文件:java
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; } } }
注意:nginx
把tomcat下面的ROOT目錄複製到/usr/local/nginx/html這個目錄下面web
nginx配置文件設置:後端
root /usr/local/nginx/html/ROOT;瀏覽器
測試反向代理和負載均衡:緩存
這樣咱們直接訪問http://192.168.127.141就出現了tomcat的頁面了,說明咱們的反向代理成功了,如圖: 兩個不一樣的頁面說明咱們的負載均衡也成功了。
測試動靜分離:
分別在兩臺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 ,不管怎樣刷新,頁面都顯示以下:
在瀏覽器中訪問http://192.168.127.141/shop/test.jsp,刷新幾回,顯示不一樣的內容,以下
從結果來看,訪問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。