shangban 发表于 2016-12-23 10:37:57

Nginx+tomcat配置负载均衡

  window下对Nginx+tomcat负载均衡做了配置尝试,将全部请求转发到tomcat,并未做静态,动态分开,图片防盗链等配置。
  

Nginx
介绍

    
Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like
协议下发行。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.
 


Window
xp环境:Nginx+Tomcat6
1、下载地址
       http://nginx.org/en/download.html,这里我们推荐下载稳定版(stable
versions),本文采用nginx-1.0.4。

2、目录结构

     
Nginx-
              
|_  conf   配置目录
              
|_  contrib
              
|_  docs 文档目录
              
|_  logs  日志目录
              
|_  temp 临时文件目录
              
|_  html 静态页面目录
              
|_  nginx.exe 主程序

     
window下安装Nginx极其简单,解压缩到一个无空格的英文目录即可,双击nginx启动,这里我安装到:D:\software\nginx-1.0.4目录,下面涉及到的tomcat也安装在此目录。


        在DOS命令下即可启动Nginx:d:  --->  cd d:\software\nginx-1.0.4 ---> start nginx
  如果要对启动的Nginx进程进行控制,也可以使用DOS命令:
  nginx -s
3、nginx.conf配置

  
Nginx配置文件默认在conf目录,主要配置文件为nginx.conf。下面是nginx作为前端反向代理服务器的配置。
  


#使用的用户和组,window下不指定
#userwww www;
#指定工作衍生进程数(一般等于CPU总和数或总和数的两倍,例如两个四核CPU,则总和数为8)
worker_processes1;
#指定错误日志文件存放路径,错误日志级别可选项为【debug|info|notice|warn|error|crit】
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
error_loglogs/error.loginfo;
#指定pid存放路径
pid      logs/nginx.pid;
#工作模式及连接数上限
events {
#使用网络I/O模型,Linux系统推荐使用epoll模型,FreeBSD系统推荐使用kqueue;window下不指定
#use epoll;
#允许的连接数
worker_connections1024;
}
#设定http服务器,利用他的反向代理功能提供负载均衡支持
http {
#设定mime类型
include       mime.types;
default_typeapplication/octet-stream;
#设定日志格式
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
log_format main '$remote_addr - $remote_user [$time_local]'   
'"$request" $status $bytes_sent'   
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"'   
'"$gzip_ratio"';   
log_format download '$remote_addr - $remote_user [$time_local]'   
'"$request" $status $bytes_sent'   
'"$http_referer" "$http_user_agent"'   
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲   
client_header_buffer_size 1k;   
large_client_header_buffers 4 4k;
#设定access log
access_loglogs/access.logmain;
client_header_timeout 3m;   
client_body_timeout 3m;   
send_timeout 3m;
sendfile      on;
tcp_nopush   on;
tcp_nodelay on;
#keepalive_timeout0;
keepalive_timeout65;
#开启gzip模块
gzipon;
gzip_min_length 1100;   
gzip_buffers 4 8k;   
gzip_types text/plain application/x-javascript text/css application/xml;
output_buffers 1 32k;   
postpone_output 1460;
server_names_hash_bucket_size 128;
client_max_body_size 8m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_vary on;
#设定负载均衡的服务器列表   
upstream localhost {
#根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
#同一机器在多网情况下,路由切换,ip可能不同   
#weigth参数表示权值,权值越高被分配到的几率越大   
server localhost:8080 weight=1;   
server localhost:9080 weight=1;   
}
#设定虚拟主机
server {
listen       80;
server_namelocalhost;
#koi8-r
charset UTF-8;
#设定本虚拟主机的访问日志
access_loglogs/host.access.logmain;
#假如访问 /img/*, /js/*, /css/* 资源,则直接取本地文档,不通过squid   
#假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/ {   
root /data3/Html;   
expires 24h;   
}
#对 "/" 启用负载均衡
location / {
root   html;
indexindex.html index.htm index.jsp;
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_read_timeout 90;   
#后端服务器数据回传时间 就是在规定时间内后端服务器必须传完所有数据   
proxy_send_timeout 90;
#代理请求缓存区 这个缓存区间会保存用户的头信息一共Nginx进行规则处理 一般只要能保存下头信息即可
proxy_buffer_size 4k;   
#同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
proxy_buffers 4 32k;   
#如果系统很忙的时候可以申请国内各大的proxy_buffers 官方推荐 *2
proxy_busy_buffers_size 64k;
#proxy 缓存临时文件的大小
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_pass http://localhost;
}
#设定查看Nginx状态的地址   
#location /NginxStatus {   
#stub_status on;   
#access_log on;   
#auth_basic "NginxStatus";   
#auth_basic_user_file conf/htpasswd;   
#}   
#error_page404            /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
#
#location ~ \.php$ {
#    root         html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_indexindex.php;
#    fastcgi_paramSCRIPT_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 {
#    denyall;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_namesomenamealiasanother.alias;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443;
#    server_namelocalhost;
#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_keycert.key;
#    ssl_session_timeout5m;
#    ssl_protocolsSSLv2 SSLv3 TLSv1;
#    ssl_ciphersALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#    ssl_prefer_server_ciphers   on;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}
}

  
 
  

    4、Tomcat配置
  

        tomcat的配置依然使用 Apache+tomcat集群配置的中配置。
  

5、验证配置与测试负载均衡

   
首先测试nginx配置是否正确,测试命令:nginx -t  (默认验证:conf\nginx.conf),也可以指定配置文件路径。
  

      最后验证配置负载均衡设置,http://localhost/或http://localhost/index.jsp。
  

      测试负载均衡 参考 Apache + Tomcat集群配置详解(2)


  

  至此window下nginx+tomcat负载均衡配置结束,关于tomcat
Session的问题通常是采用memcached,或者采用nginx_upstream_jvm_route ,他是一个 Nginx 的扩展模块,用来实现基于
Cookie 的 Session Sticky
的功能。如果tomcat过多不建议session同步,server间相互同步session很耗资源,高并发环境容易引起Session风暴。请根据自己应用情况合理采纳session解决方案。
  

  

  下面几篇不错的文章:
       http://czllfy.iyunv.com/blog/510295
       http://www.blogjava.net/Alpha/archive/2011/06/21/352745.html
       http://www.jtben.com/document/4440
       http://tmsoft.lsxy.com/index.php?load=read&id=938
  张宴的Blog:http://blog.s135.com/nginx_cache/
页: [1]
查看完整版本: Nginx+tomcat配置负载均衡