Nginx+Tomcat集群與負載均衡

jopen 12年前發布 | 95K 次閱讀 Nginx Web服務器

架構描述

前端一臺nginx服務器做負載均衡器,后端放N臺tomcat組成集群處理服務,通過nginx轉發到后面(注:沒做動靜分離,靜態動態全部都轉給tomcat)
優點:實現了可彈性化的架構,在壓力增大的時候可以臨時添加tomcat服務器添加到這個架構里面去.

先修改nginx.conf配置

如想了解Nginx負載均衡策略,請查閱Nginx負載均衡策略.

user nginx;
worker_processes 10;
worker_rlimit_nofile 100000;

error_log
/var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    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 /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    gzip on;
    gzip_static on;
    gzip_comp_level 5;
    gzip_min_length 1024;
    keepalive_timeout 65;
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;

    upstream www.wp.com {
        #此處為輪詢策略 換成你自己的tomcat地址
        #可以根據自己的需求配置多個tomcat,當某個地址無效時,nginx會自動切換
        server 192.168.5.206:8081;
        server 192.168.5.206:8082;
    }

    server {
        #配置對應的端口與域名
        listen 80;
        server_name www.wp.com;

        #charset koi8-r;

        #access_log logs/host.access.log main;
        location / {
            root html;
            index index.html index.htm;
            #配置上面的server name
            proxy_pass http://www.wp.com;
            proxy_set_header X-Real-IP $remote_addr;
        }

        location ~ ^/(WEB-INF)/ {
            deny all;
        }

        error_page 404 /404.html;

        location = /404.html {
            root /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }
}

修改tomcat配置

找到TOMCAT_HOME/conf/server.xml,在host標簽處加入Context標簽
注意:如果是單機啟動多tomcat需要改變對應的port,否則會啟動不了,多機tomcat無此情況

<Context docBase="/opt/tomcat/cluster01/webapps/ROOT" path="" />

到此重啟tomcat與nginx服務即可.

來自:http://www.pigg.co/nginx-tomcat-cluster-andload-balance.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!