Nginx+tomcat 做负载均衡

一、

1、将tomcat 的server.xml文件中所有端口号都改为不同。
2、Nginx 的nginx.conf文件中
    http {}增加如下内容

     upstream sp.imichat.com{ 
       server 127.0.0.1:8080 weight=2; 
       server 127.0.0.1:8088 weight=2; 
       ip_hash;
     }

server {} 修改信息:


listen 80;
server_name sp.imichat.com;

#charset koi8-r;

#access_log logs/host_access_log main;

        location /{
          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;
          proxy_pass http://sp.imichat.com;
        }

例:
#user  nobody;
worker_processes  1;

events {
    worker_connections  51024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

     upstream localhost { 
       server 127.0.0.1:8080 weight=2; 
       server 127.0.0.1:8088 weight=2; 
       ip_hash;
     }

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location /{
                        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;
   proxy_pass http://localhost;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }}


二、

参数描述

检测nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t -c nginx.conf
-c 配置文件路径

-g Set global directives. (version >=0.7.4)

-t 检测文件是否正确不执行

-v Print version.

-V Print nginx version, compiler version and configure parameters.

编译时如果使用了–with-debug编译,还可以使用error_log file [ debug_core| debug_http | debug_event …] 来获得debug信息

通过信号对 Nginx 进行控制

Nginx 支持下表中的信号:

信号名 作用描述 
TERM, INT 快速关闭程序,中止当前正在处理的请求 
QUIT 处理完当前请求后,关闭程序 
HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求 
USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件 
USR2 平滑升级可执行程序 
WINCH 从容关闭工作进程

有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。

配置:

use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];FreeBSD使用kqueue,Linux选epoll.
worker_connections number    每个worker的最大连接数
Maxclient = work_processes * worker_connections

nginx的upstream目前支持4种方式的分配

1、轮询(默认)

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

2、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

3、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4、url_hash(第三方)

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

代理
只需要在nginx的配置文件中增加虚拟主机,然后加入
\proxy_pass http://localhost:8000;

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

每个设备的状态设置为:
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 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

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

FASTCGI配置:

请将以下内容保存为fastcgi_params文件,保存于/usr/local/nginx/conf下(Ubuntu可保存于/etc/nginx下),他为我们的FastCGI模块设置了基本的环境变量:

#fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE    nginx;
fastcgi_param QUERY_STRING       $query_string;
fastcgi_param REQUEST_METHOD     $request_method;
fastcgi_param CONTENT_TYPE       $content_type;
fastcgi_param CONTENT_LENGTH     $content_length;
fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param REQUEST_URI        $request_uri;
fastcgi_param DOCUMENT_URI       $document_uri;
fastcgi_param DOCUMENT_ROOT      $document_root;
fastcgi_param SERVER_PROTOCOL    $server_protocol;
fastcgi_param REMOTE_ADDR        $remote_addr;
fastcgi_param REMOTE_PORT        $remote_port;
fastcgi_param SERVER_ADDR        $server_addr;
fastcgi_param SERVER_PORT        $server_port;
fastcgi_param SERVER_NAME        $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS    200;请特别注意加粗的一行,PHP-CGI特别需要此行信息来确定PHP文件的位置。

另外需要在PHP-CGI的配置文件(Ubuntu 上此配置文件位于/etc/php5/cgi/php.ini)中,打开cgi.fix_pathinfo选项:

cgi.fix_pathinfo=1;这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。

接下来在nginx的配置中针对php文件配置其利用FastCGI进程来执行:

server {
index index.php;
root /usr/local/nginx/html;

    location ~ .*.php$ {
include /usr/local/nginx/conf/fastcgi_params; #请根据自己保存的路径进行设置
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置
}
}通知Nginx重新载入配置:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`Ubuntu用户可以使用init脚本:sudo /etc/init.d/nginx reload

然后启动php-cgi -b 127.0.0.1:9000

如果出现No input file specified表示SCRIPT_FILENAME设置的有问题。
使用lighttpd的 spawn-fcgi

get http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure #编译
make
cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #取出spawn-fcgi的程序下面我们就可以使用 spawn-fcgi 来控制php-cgi的FastCGI进程了

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi参数含义如下

-f <fcgiapp> 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置 
-a <addr> 绑定到地址addr 
-p <port> 绑定到端口port 
-s <path> 绑定到unix socket的路径path 
-C <childs> 指定产生的FastCGI的进程数,默认为5(仅用于PHP) 
-P <path> 指定产生的进程的PID文件路径 
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

#运行用户
user   nobody nobody;
#启动进程
worker_processes   2;
#全局错误日志及PID文件
error_log   logs/error.log notice;
pid        logs/nginx.pid;
#工作模式及连接数上限
events {
use epoll;
worker_connections    1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include    conf/mime.types;
default_type   application/octet-stream;
#设定日志格式
log_format main        '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$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;
#开启gzip模块
gzip on;
gzip_min_length   1100;
gzip_buffers 4 8k;
gzip_types    text/plain;
output_buffers   1 32k;
postpone_output   1460;
#设定access log
access_log   logs/access.log   main;
client_header_timeout   3m;
client_body_timeout 3m;
send_timeout       3m;
sendfile             on;
tcp_nopush              on;
tcp_nodelay          on;
keepalive_timeout   65;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80   weight=1;
server 192.168.8.3:80   weight=6;
}
#设定虚拟主机
server {
listen       80;
server_name 192.168.8.1 www.yejr.com;
charset gb2312;
#设定本虚拟主机的访问日志
access_log   logs/www.yejr.com.access.log   main;
#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/   {
root /data3/Html;
expires 24h;
}

猜你喜欢

转载自haiker.iteye.com/blog/1147656