Nginx-Tomcat负载均衡

Nginx-Tomcat负载均衡

Java软件开发,在更新版本的时候,往往要重启Tomcat服务;如果资源量少,重启服务几乎是瞬间完成,但是如果资源量大的话,重启会比较慢,影响网站的正常运营。下面介绍一个Nginx配合Tomcat的负载均衡方案,以便实现网站的不断点运营。

 

1. Servers:

server_1: 192.168.1.1: Nginx任务分发服务器

server_2: 192.168.1.2: Tomcat服务器_1

server_3: 192.168.1.3: Tomcat服务器_2

2. Nginx服务器的搭建

  Nginx的编译安装,参照:http://www.cnblogs.com/llius/p/5105240.html,在这里就不做介绍;

  配置参数添加(供参考):

 

user nobody nobody;
worker_processes 2;   //根据服务器cpu核数修改
error_log /usr/local/nginx/logs/nginx_error.log crit;   //错误日志,有问题找它
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
    include vhosts/*.conf;    //打开$basedir/conf/vhosts目录里面的配置开关

server
{
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;   //nginx的默认网页目录,默认是打开index.html
## 下面是添加php解析
    location ~ .php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
##下面是解析php的默认目录配置,在这里是:/usr/local/nginx/html
        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
}
}

 

   上面的配置中,include vhosts/*.conf是非常重要的,它是打开请求分发功能必须的环节,因为我们需要在$basedir/conf/vhosts目录下面建立N个conf文件,来配置我们想要实现的功能。

3. 禁掉Nginx默认主机

  默认主机禁掉以后,方便进行后面的任务分发,而通过server_1的ip在浏览器访问的时候,得到的返回码全部为403.

# mkdir /usr/local/nginx/conf/vhosts   //如果没有的话,就创建这个目录,这里nginx的base_dir=/usr/local/nginx
# cd !$
# vim default.conf   //写入如下配置

server
  {
	listen 80 default_server; ## 默认的虚拟主机
	server_name 123.com;
	index index.html index.htm index.php;
	root /tmp/tmp;   //创建这个空目录
	deny all;
  }

# mkdir -p /tmp/tmp
# /etc/init.d/nginx restart

   如此,以server_1的ip,在浏览器访问的时候,是禁止访问的:

403 Forbidden
-----------------------------------------------------------------------------------
nginx/1.8.0

 4. 开启请求分发

# cd /usr/local/nginx/conf/vhosts
# vim lb.conf    //写入如下配置

upstream test { ip_hash;  //test为模块名
server 192.168.1.2:8080;   //默认端口是80,如果web服务器为httpd或者nginx,可以不加:80,直接是ip形式即可
server 192.168.1.3:8080;   //但是因为是tomcat做web服务器,所以,要指定:8080
}

server { listen 80;   //这里是nginx监听的端口,是我们代理服务器监听的web端口,自然是:80
server_name www.lius.com;   //设定的访问域名,键入此可访问到Nginx服务器
location / {
proxy_pass http://test/;   //代理到上面提到的test模块
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
}

   保存退出后,重启nginx服务,可以实现请求的分发。那么,如何对我们的配置结果进行测试呢?

5. 测试请求分发功能

  要测试分发功能,需要在Tomcat服务器_1和2上面,做一些配置:

  【1】确定tomcat的默认监听端口是8080, 以为请求分发服务器上面,已经配置了8080.

# vim /usr/local/tomcat/conf/server.xml

  搜索protocol="HTTP/1.1,找到所在行,看看Connector port="8080"是否为8080, 不是的话改为8080.

  【2】新建index.html文件

  在Tomcat服务器_1和2上,分别建立index.html文件,写入不同的内容,如:

# server_1
vim /usr/local/tomcat/webapps/ROOT/index.html  //键入11111111

# server_2
vim /usr/local/tomcat/webapps/ROOT/index.html  //键入22222222

   重启两个服务器的tomcat进程;由于tomcat的启动、停止都要用绝对路径或者相对路径,比较繁琐,所以可以用alias的方式,实现简单操作:

# vim ~/.bashrc   //在alias组里面键入如下内容,当然,alias可以自定义,按照自己的操作习惯
alias tcstart='/usr/local/tomcat/bin/startup.sh'
alias tcstop='/usr/local/tomcat/bin/shutdown.sh'
alias tcrestart='/usr/local/tomcat/bin/shutdown.sh;/usr/local/tomcat/bin/startup.sh'

# source ~/.bashrc

  【3】访问测试:

  首先,在window里面,需要将hosts文件里面,添加我们域名的指向ip:

  打开c:/windows/system32/drivers/etc/hosts, 添加一行:192.168.1.1 www.lius.com,需要管理员权限,用写字板打开可以保存;不行的话,用notpad++,保存会提示切换到管理员模式,可以实现保存。

  然后,在浏览器键入:www.lius.com,在nginx、Tomcat配置文件修改后,服务都重新启动的前提下,刷新我们的浏览器界面,会一直在11111111和22222222之间切换。

  如果我们想让一个服务器承载更多的请求,需要更改Nginx请求分发的权重。那么,如何更改nginx的权重呢?

# vim /usr/local/nginx/conf/vhosts/lb.conf   //在下面模块做操作

upstream lius {
        server 192.168.1.2:8080 weight=2;
        server 192.168.1.3:8080 weight=1;
}

   配置完要检查一下配置文件是否错误,我们自定义的Nginx启动脚本,可以实现这个功能:

# /etc/init.d/nginx configtest
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# /etc/init.d/nginx restart

   这样,在请求的时候,两次11111111,一次22222222,循环往复。

原文地址:https://www.cnblogs.com/llius/p/5105614.html