Nginx+多tomcat7的负载均衡和动静分离配置

简介

  所谓动静分离就是通过nginx(或apache等)来处理用户端请求的图片、html等静态的文件,tomcat(或weblogic)处理jsp、do等动态文件,从而达到动静页面访问时通过不同的容器来处理。nginx处理静态页面效率远高于tomcat,而tomcat擅长动态页面处理,这样一来就能更好的提高并发,处理性能。
  所谓负载均衡就是、采用nginx的proxy_pass将location做动静分离后的jsp、do等jsp程序文件分发到后端upstreamd模块中tomcat集群上,rewrite做正则分发,此时也将应用到nginx经典之处的IP哈希(ip_hash)模块,这样每个访客固定访问一个后端web服务器,可以解决session的问题。
  架构描述
  1. 一台nginx:做静态的html服务器和负载均衡
  2. 两台tomcat服务器每台服务器装有2个tomcat服务,使其支持动态jsp程序和.do文件

1.安装JDK,tomcat,nginx

pass

2.配置tomcat

 配置tomcat,修改server.xml 文件

这3个关键子地方的port, 很好记, 我现在修改后都是以 81开头的, 而之后的tomcat-8-2, 我就会以82开头:

<Server port="8105" shutdown="SHUTDOWN">
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8180" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443”/>

8105" shutdown="SHUTDOWN">

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8180" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443”/> 

3. 修改nginx配置

找到nginx.conf 文件不同系统位置不一样

# 定义Nginx运行的用户 和 用户组 如果对应服务器暴露在外面的话建议使用权限较小的用户 防止被入侵
#user  nobody;
#Nginx进程数, 建议设置为等于CPU总核心数
worker_processes  2;
#开启全局错误日志类型
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程文件
#pid        logs/nginx.pid;


events {
 #使用epoll模型提高性能
 use epoll;
 #单个进程最大连接数
 worker_connections  1024;
}


http {
 #扩展名与文件类型映射表
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #  动态服务器组
    upstream localhost {
    #ip_hash; 
    server localhost:8180 max_fails=3 weight=1 fail_timeout=60s;
    server localhost:8280 max_fails=3 weight=1 fail_timeout=60s;
    }

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        index index.jsp index.php index.do index.jsp;
        # 日志设置
        access_log  logs/host.access.log;
        error_log   logs/host.error.log;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://localhost; 
            proxy_set_header X-Real-IP $remote_addr; 
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
            proxy_redirect HOST default; 
        }

        location ~ \.(jsp|do)$ {        #jsp,do等页面交由tomcat处理
            proxy_pass http://localhost;
            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;
            }
    }


    # 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
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

4.Nginx upstream的几种分配方式

http://blog.csdn.net/chruan/article/details/8864024

5. 配置详解

#这里为后端服务器cluster应用集群配置,根据后端实际情况修改即可,cluster_app为负载均衡名称,可以任意指定
#但必须跟server段 proxy_pass定义一致,否则不能转发后端的请求。
upstream cluster_tomcat {
   ip_hash;
   server   10.10.141.30:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.30:8081 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.31:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.31:8081 weight=1 max_fails=2 fail_timeout=30s;
 }
#这里为后端APP应用负载均衡配置,根据后端实际情况修改即可。tdt_app为负载均衡名称,可以任意指定
upstream cluster_nginx {
    ip_hash;
    server   10.10.141.40:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.40:8081 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.41:8080 weight=1 max_fails=2 fail_timeout=30s;
    server   10.10.141.41:8081 weight=1 max_fails=2 fail_timeout=30s;
 }

####www.test1.cn
server
  {
    listen       80;
    server_name  www.test1.com;
    index  index.html index.php index.htm index.jsp index.do default.do;
#配置发布目录为/data/www/wugk
    root  /data/www/wugk;
#动态页面交给http://cluster_tomcat,也即我们之前在nginx.conf定义的upstream cluster_tomcat 均衡
    location ~ .*\.(jsp|do)?$
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://cluster_tomcat;
    }  
    location ~ \.(php|php5)?$ 
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http:cluster_nginx;
    }

#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
    root /data/www/wugk;
    #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
    expires      3d;
    }

参考链接

http://asaderas.blog.51cto.com/9344932/1682802
http://lanjingling.github.io/2015/10/28/nginx-tomcat-cluster/
http://blog.jobbole.com/95376/
http://wgkgood.blog.51cto.com/1192594/1094236

你可能感兴趣的:(工具)