天天看點

Struts架構下Nginx與Tomcat容器的深度整合實踐

本部署架構設計的目的是使用Nginx處理使用者向伺服器發起靜态頁面的請求如html、圖檔、css樣式表及js等,使用tomcat處理使用者向伺服器發起的動态請求項進而提高我們伺服器的傳遞能力和深度挖掘伺服器的性能。

一、安裝軟體準備

1.程式運作環境軟體 jdk-6u31-linux-i586.bin

2.web容器apache-tomcat-6.0.35

3. 正規表達式pcre-7.9.tar.gz

4. nginx-1.3.3.tar.gz

5. 壓測webbench-1.5.tar.gz (該軟體也可以不安裝隻是一款壓力測試軟體)

二、使用者管理

1.添加www使用者組 groupadd www

2.添加www使用者 useradd www –g www

三、安裝媒體

1.tomcat免安裝軟體 是以cp到www使用者下後使用tar  -xvf tomcat.tar 即可

2.安裝jdk程式包

首先賦予其執行權限 chmod u+x jdk-6u31-linux-i586.bin後進行安裝 ./ jdk-6u31-linux-i586.bin

3.設定運作環境變量

在etc目錄内追加以下内容到profile檔案

JAVA_HOME=/usr/local/jdk1.6.0_31

export JAVA_HOME

PATH=$JAVA_HOME/bin:$PATH

export PATH

CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export CLASSPATH

4.重新加載source profile後運作java指令測試是否正确進行配置。如果傳回了java相關資訊則證明配置已經生效

5.進入tomcat的啟動目錄cd  /home/www/tocmat6/bin

6.啟動tomcat 服務sh startup.sh

8.優化跳轉頁面,該設定可以使你的域名直接跳轉至你的業務層

a)cd  /home/www/tocmat6/conf

b)修改server.xml檔案的資訊如下所示

#在Host的配置項内進行配置

10.部署Nginx軟體

tar –zxvf  nginx-1.3.3.tar.gz

cd nginx-1.3.3

./configure --with-http_stub_status_module --with-http_ssl_module

提示缺少PCRE library

解決方法安裝pcre-7.9.tar.gz程式

tar –zxvf pcre-7.9.tar.gz

cd pcre-7.9

./configure

make && make install(在root使用者下部署)

11.重新編譯并安裝nginx軟體

make && make install

12.配置nginx服務

(一)、首先touch一個反向代理檔案proxy.conf并追加以下内容到配置檔案内

#add by fengzhanhai

# proxy.conf

proxy_redirect off;

proxy_set_header Host $host;

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;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

(二)、修改nginx.conf主配置檔案

user www www; #運作該服務的使用者及使用者組 各位同學記得1024端口下的服務要在root下啟動哦

#user  nobody;

worker_processes  8; #根據你的伺服器硬體性能設定

error_log  /home/www/nginx/logs/error.log; #錯誤日志的存放位置 很重要哦

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        /home/www/nginx/logs/nginx.pid; #伺服器每次啟動的id都會寫到該檔案哦

worker_rlimit_nofile 65535; #并發連接配接數上限

events {

    use epoll;             #開啟epoll模式

    worker_connections  65535;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    include /home/www/nginx/conf/proxy.conf;

    #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;

    #設定請求緩存

    server_names_hash_bucket_size 128;

    client_header_buffer_size 32k;

    large_client_header_buffers 8 32k;

   # client_max_body_size 8m;

    sendfile        on;

    tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    tcp_nodelay on;

    #gzip  on;

    server {

        listen       80;

        server_name  localhost;

        charset utf-8;

        index index.html index.htm index.jsp login.jsp; #設定通路的預設首頁位址

        #access_log  logs/host.access.log  main;

        root /home/www/tomcat6/webapps/MMS;   #設定靜态資料通路的位置

        location ~.*\.(jsp|do)$                       #設定動态資料的請求url

        {

        index index.jsp;

        proxy_pass http://localhost:8080; #動态請求轉向處理

        }

        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|html)$ #靜态資料儲存時效

          expires 30d;

        location ~.*\.(js|css)?$   #js和css儲存時效

         expires 1h;

      location ~(favicon.ico) 

         log_not_found off;

         expires 30d;

         break;

      location /status  #統計Nginx運作狀态

      {

        stub_status on;

        access_log /home/www/nginx/logs/status.log;

        auth_basic "NginxStatus";

       }

      # log_format access '$remote_addr - $remote_user[$time_local] "$request"'

      # '$status $body_bytes_sent "$http_referer"'

      # '"$http_user_agent" $http_x_forwarded_for';

      # access_log /home/www/nginx/logs/localhost.log access; #設定通路日志存放路徑

       # location / {

       #     root   html;

       #     index  index.html index.htm;

       # }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #location ~ /\.ht {

        #    deny  all;

    }

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

    # HTTPS server

    #    listen       443;

    #    server_name  localhost;

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

13. stub_status統計Nginx運作狀态的用法介紹

   #該設定的用法為

      Active connections: 1

server accepts handled requests

      22 22 83

Reading: 0 Writing: 1 Waiting: 0

     active connections對後端發起的活動連接配接數

server accepts handled requests — nginx 總共處理了 22 個連接配接, 成功建立 22 次握手 (證明中間沒有失敗的), 總共處理了83 個請求 (平均每次握手處理了 4個資料請求)

reading讀取到用戶端的Header資訊數

writing傳回給用戶端的Header資訊數

waiting已經處理完正在等候下一次請求指令的駐留連接配接

14.出現如下錯誤在使用nginx代理通路原有服務時圖檔無法正常顯示背景如下錯誤

2012/08/22 11:29:27 [error] 14389#0: *2 "/home/www/tomcat/webapps/sdmtv/index.html" is forbidden (13: Permission denied)

2012/08/2211:29:27[error]14389#0: *2 open() "/home/www/tomcat/webapps/sdmtv/favicon.ico" failed (13: Permission denied)

解決方法如下将你的tomcat目錄權限設定 為chmod u+7 tomcat6

四、後續完善之壓力測試篇(需要的朋友可以從其他伺服器或者你的pc上部署一款功能類似的軟體進行測試也可以)

2.解壓下載下傳後的壓縮包 tar –zxvf webbench-1.5.tar.gz

3.編譯和安裝壓力測試程式webbench make && make install

6.通過top和free指令檢視主機資源使用情況