linux nginx1.9.2+tomcat8 配置 多tomcat负载均衡,并使静态文静不通过tomcat解析

本项目完成的任务,nginx1.9.2+tomcat8 配置 多tomcat负载均衡,并使静态文静不通过tomcat解析,而是直接通过nginx直接返回静态文件数据,提高响应时间,提高并发量。注意,在nginx.conf设置静态文件路径时,要确保此路径在本用户下有访问权限,最好设为chmod  7777 filename。

 

user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    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 lhy {
          #配置两台tomcat负载均衡,ip:port地址为tomcat地址。weight为权重,权重越大,访问概率越大,
         server 192.168.1.106:8080 weight=10;
         server 192.168.1.106:8090 weight=10;
         }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

      #  access_log  logs/host.access.log  main;
    location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
             {
                  root /home/lhy/nginx_data/;
                  #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
                   expires      7d;
                                                   
              }
       location ~ (\.jsp)|(\.do)$
     {    
             #root   html;
             #index  index.jsp index.htm;
         #lhy是 upstream 后面的名字 lhy
             proxy_pass http://lhy;
             #localhost是nginx服务器的主机地址,如果不写此句,会导致静态文件访问路径为http://lhy,导致找不到地址
           proxy_set_header Host localhost;  
                #forwarded信息,用于告诉后端服务器终端用户的ip地址,否则后端服务器只能获取前端代理服务器的ip地址。
            proxy_set_header Forwarded $remote_addr;  
        }

     # / 表示匹配所有地址,默认最大前缀匹配,如果其他没有匹配的才会匹配
        location /{
             root  /home/lhy;
              index  index.html index.htm;
               }
            
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #错误页面地址,500 502 503 504错误的地址  
        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
        #
        #location ~ \.php$ {
        #    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
    #
    #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;
    #    }
    #}

}

路径转发

server {
        listen       80;
        server_name   115.28.77.24;#本服务器ip地址

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       # location /vpn/ {
       location /{
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://10.8.0.66:8090/;
           #当地址最后加上 /时,匹配路径的 yanshi 不会加到转发路径中
            proxy_set_header X-Forwarded-For $remote_addr;
           #当下面这句话不加,Host $host; 会导致post请求参数丢失
            proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
        }
}

同一服务器部署多个tomcat时,存在端口号冲突的问题,所以需要修改tomcat配置文件server.xml,以tomcat7为例。

  • 首先了解下tomcat的几个主要端口:

<Connector port="8080" protocol="HTTP/1.1"  connectionTimeout="60000"  redirectPort="8443" disableUploadTimeout="false"  executor="tomcatThreadPool" URIEncoding="UTF-8"/>

其中8080为HTTP端口,8443为HTTPS端口

<Server port="8005" shutdown="SHUTDOWN">   

8005为远程停服务端口

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 

8009为AJP端口,APACHE能过AJP协议访问TOMCAT的8009端口。

  • 部署多个tomcat主要修改三个端口:

1.HTTP端口,默认8080,如下改为8081

[html] view plain copy

  1. <Connector port="8081" protocol="HTTP/1.1"   
  2.                connectionTimeout="60000"   
  3.                redirectPort="8443" disableUploadTimeout="false"  executor="tomcatThreadPool"  URIEncoding="UTF-8"/>  

2.远程停服务端口,默认8005,如下改为8006

[html] view plain copy

  1. <Server port="8006" shutdown="SHUTDOWN">......  

3.AJP端口,默认8009,如下改,8010

[html] view plain copy

  1. <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> 

  nginx与tomcat的结合,主要用的是nginx中的upstream,后端可包括有多台tomcat来处ginx的upstream目前支持4种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
例如:
    upstreambakend {
        server 192.168.0.14 weight=10;
        server 192.168.0.15 weight=10;
   }

2、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。
例如:
    upstreambakend {
        ip_hash;
        server 192.168.0.14:88;
        server 192.168.0.15:80;
   }

3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
    serverserver1;
    serverserver2;
    fair;
}

4、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
    serversquid1:3128;
    serversquid2:3128;
   hash   $request_uri;
    hash_methodcrc32;
}

tips:

upstream bakend{#定义负载均衡设备的Ip及设备状态
ip_hash;
    server127.0.0.1:9090 down;
    server127.0.0.1:8080 weight=2;
    server127.0.0.1:6060;
    server127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://bakend/;

每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲clientpost过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

时间: 2024-06-12 10:12:56

linux nginx1.9.2+tomcat8 配置 多tomcat负载均衡,并使静态文静不通过tomcat解析的相关文章

linux下Nginx+Tomcat负载均衡配置方法_nginx

Nginx+tomcat是目前主流的java web架构,如何让nginx+tomcat同时工作呢,也可以说如何使用nginx来反向代理tomcat后端均衡呢?直接安装配置如下: 1.JAVA JDK安装: #下载相应的jdk软件包,然后解压安装,我这里包名称为:jdk-7u25-linux-x64.tar.gz tar -xzf jdk-7u25-linux-x64.tar.gz ;mkdir -p /usr/java/ ;mv jdk1.7.0_25/ /usr/java/ 下. #然后配置

Apache(mod_proxy)+Tomcat负载均衡配置

   要想使用Apache做反向代理,实现Tomcat负载均衡,首先你要知道有几种连接方式,怎样连接的,还有集群方面比较关心的问题,如何保持Session持久化.原来部署过mod_jk方式的,相对http_proxy比较复杂点,这次就部署测试下http_proxy,因为本公司大多数系统都是Ubuntu,所以这次也用Ubuntu系统吧!与CentOS相比配置基本相同,只是apache安装和配置路径不同.那就简单介绍下,让没有搞过Apche+Tomcat集群的博友们心里有个底. 1.Tomcat+A

tomcat集群-nginx 反向代理三个本地tomcat 负载均衡配置 无法加载js css 图片等 也不报错

问题描述 nginx 反向代理三个本地tomcat 负载均衡配置 无法加载js css 图片等 也不报错 打开速度非常慢,到最后js css无法加载是什么问题 我的项目是ssh+jquery+easyui的 单独访问tomcat完全正常 访问nginx就这样了 .求大神指点啊 访问webapp根目录是可以的 我的页面都是在web-inf目录下的貌似不行 nginx配置如下: #user nobody; worker_processes 1; #error_log logs/error.log;

Apache+Tomcat负载均衡问题集锦

之前在windows 环境下搭建了下apache+tomcat负载均衡(不会的可以参考之前的文档,文档对于linux和windows都适用),一帆风顺,没有出现任何问题,今天尝试着在linux下搭建了下,其中遇到几个问题,在这里给大家分享下. linux环境,三个redhat6.5 x86_64,其中192.168.1.2安装apache,192.168.1.3和192.168.1.4安装tomcat,这里不再介绍配置,如果有不会的,可以参考前边的文章. 问题一: SESSIONID随着页面刷新

Apache + Tomcat 负载均衡 session复制

转自:http://blog.csdn.net/cssmhyl/article/details/8455400 http://snowolf.iteye.com/blog/743611 Apache 和 Tomcat原本就是一家,更是一家亲!Apache与Tomcat整合,无非是将Apache作为前端根据请求路径.端口.代理分发给多个Tomcat,以到达转发和负载均衡的目的!同时,通过Apache和Tomcat相互作用,进行粘性会话,会话拷贝构建集群!这一切的最终结果就是"云服务"!不

apache2.2+tomcat负载均衡在SSH2项目中session无法共享!!!!!!

问题描述 apache2.2+tomcat负载均衡在SSH2项目中session无法共享!!!!!! 使用apache2.2和三个tomcat实例在同一台机器配置负载均衡成功,基本软件:apache.2.225Tomcat8.0.20Tomcat-connectors-1.2.40使用如下Jsp页面时显示session要以复制且sessionId相同,但是加载实际SSH2实际项目,则发现session丢失且每次都创建新的session请有类似配置经验或解决方案的同仁不吝赐教!<% HttpSes

apache + tomcat 负载均衡 失败了!

问题描述 apache + tomcat 负载均衡 失败了! 直接访问tomcat下面的test项目完全没问题,但是通过apache访问就出错了. 废话不多说,直接上我的配置文件.1.apache配置文件apache24/conf目录httpd.conf最后一行mod_jk.conf配置文件workers.properties配置文件 2.tomcat配置文件tomcat1下的conf/server.xml配置文件另外,每个tomcat的端口号都不一样(包括AJP端口.http端口.server

nginx tomcat负载均衡

问题描述 nginx tomcat负载均衡 我用nginx + 2个tomcat做的负载均衡,2个tomcat用的一套项目,现在需要做一个定时器任务,但是启动后两个tomcat都会去执行,也就是定时任务执行了两次. 如何才能避免这个问题呢?获得如何能获得当前执行的服务器是哪个,从而进行判断只执行一个. 解决方案 Nginx安装安装路径: /usr/local/nginx安装依赖包[root@localhost /]# yum install gcc-c++ [root@localhost /]#

nginx+tomcat负载均衡刷新页面不会更换主机

问题描述 nginx+tomcat负载均衡刷新页面不会更换主机 用nginx+tomcat+memcached做负载均衡,写了一个测试页面,用浏览器访问, 刷新页面每次都是访问的同一个tomcat,是什么问题啊? 配置文件: 测试地址: 解决方案 它的负载均衡要是基于IP地址,你的IP没变,自然每次都可能分配到同一个服务器.