失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Nginx + Tomcat 负载均衡集群配置

Nginx + Tomcat 负载均衡集群配置

时间:2021-12-20 17:27:01

相关推荐

Nginx + Tomcat 负载均衡集群配置

摘要

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

Nginx发布

当前稳定版: Nginx 0.6.35 (更新记录) ( 1月26日)

当前开发版: Nginx 0.7.44 (更新记录) ( 3月23日)

历史稳定版: Nginx 0.5.37 (更新记录) ( 7月7日)

Nginx文档地址

/NginxChs

注意点

1 Nginx并不支持Windows,只能在Linux,Unix,BSD 系统下安装使用

2 Nginx本身是一个HTTP和反向代理服务器,无法像Apache一样通过安装各种模块来支持不同的页面脚本(PHP,CGI)

3 Nginx支持简单的负载均衡和容错

4 Nginx支持作为基本HTTP 服务器的功能,例如日志,压缩,SSL,虚拟主机等。

环境

RedHat Linux Enterpriser Server 5

Nginx-0.6.31

( 稳定版本)

JDK 1.6

Tomcat 6.0.18

安装

创建用户组和帐号

#/usr/sbin/groupadd www -g 48

#/usr/sbin/useradd -u 48 -g www www

#wget http://sysoev.ru/nginx/nginx-0.6.31

.tar.gz

#tar zxvf nginx-0.6.31

.tar.gz

#cd nginx-0.6.31

#./configure --with-http_stub_status_module --prefix=/usr/local/nginx

--user=www --group=www

#make

#make isntall

#--with-http_stub_status_module 是为了启用nginx的status功能,用来监控Nginx的当前状态安装后,有四个子目录conf/nginx.conf html logs sbin/nginx

确保系统80端口没有被其他程序占用,运行sbin/nginx启动,浏览器访问。

#/usr/local/nginx/sbin/nginx

编译rewrite模块

需要正则表达式,需要安装pcre包

#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz

#tar zxvf pcre-7.7.tar.gz

#cd pcre-7.7

#./configure

#make

#make install

程序运行参数:

Nginx安装后只有一个程序文件,使用参数和系统信号机制对Nginx进程本身进行控制。

-c <path_to_config>:使用指定的配置文件

-t :测试配置文件是否正确,在运行时需要重新加载配置文件,用来测试所修改配置文件是否有错误语法

-v :显示版本号

-V :显示版本号和编译环境信息和编译参数

#sbin/nginx -t -c conf/nginx2.conf

通过信号对Nginx进行控制:支持的信号:

TERM 快速关闭彻骨女婿,中止当前正在处理请求INTQUIT 处理完当前请求后,关闭程序HUP 重新加载配置,并开始启动新的工作进程,关闭旧的进程,此操作不会中断请求

USER1 重新打开日志文件,用于切换日志,如:每天生成一个新的日志文件

USER2 平滑升级可执行程序

WINCH 从容关闭工作进程

两种方式:

1 通过logs下的nginx.pid 查看当前运行的进程id ,kill - XXX <pid> 来控制进程。XXX就是信号明。

2 也可以使用killall 来完成。如killall -s HUP nginx 来重新加载配置文件。

配置

conf/nginx.conf文件:

user www www; # 工作进程的宿主

worker_processes 8; # 工作进程数,一般跟CPU核数目相同

#error_log logs/error.log; debug 模式

error_log logs/error.log notice;

#error_log logs/error.log info;

pid logs/nginx.pid;

gzip on; #打开gzip模式

gzip_camp_level 5; #压缩级别 1-9 ,1 最快,9最慢

gzip_min_length 1100;

gzip_buffers 4 8 k;

worker_rlimit_nofile 51200;

events {

use epoll; # Linux下性能最好的event

worker_connections 51200; #每个进程允许最大的连接数

}

#access_log logs/access.log main; #日志文件名

upstream tomcat{

server 192.168.0.119:5050 down;

server 192.168.0.117:5050 weight=1;

server 192.168.0.142:5050 weight=1;

}

include /usr/local/nginx/conf/proxy.conf;

location / {

root html;

index index.html index.htm;

proxy_pass http://tomcat;

}

location /NginxStatus {

stub_status on;

access_log off;

allow all;

#auth_basic "status";

#auth_basic_user_file conf/htpasswd;

}

# 静态文件和图片服务器时使用

location ~ ^/p_w_picpaths/{

root /opt/webapp/p_w_picpaths;

}

location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)${

root /opt/webapp;

access_log off;

expires 24h;

}

expires 60s;#30m

24h 1d max off

location / {

proxy_pass http://localhost:8080;

proxy_set_header X-Real-IP $remote_addr;

}

location / {

proxy_pass http://tomcat;

proxy_set_header X-Real-IP $remote_addr;

}

#Nginx使用最简单的平均分配规则给集群的节点。若一个失效,或重新起效时,Nginx会自己处理状态的变化。

proxy.conf 文件

#!nginx (-)

# proxy.conf

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_send_timeout 90;

proxy_read_timeout 90;

proxy_buffers 32 4k;

#sbin/nginx -t

#ulimit -SHn 51200

#sbin/nginx

#kill -HUP 'cat /usr/local/nginx/logs/nginx.pid'

#重新加载新的配置文件

监控:

http://localhost/NginxStatus

active connections :当前正在处理的活动连接数。

server accepts handled requests:总共处理的n个连接,成功创建n次握手(证明中间没有

失败的),总共处理了n个请求。

reading: 读取到客户端的Header信息数。

writing: 返回给客户端的Header信息数。

waiting: 开启keep-alive 情况下,该值等于active -(reading+writing),nginx已经在处

理完成正在等候下一次请求指令的驻留连接。

总结:

一般可以对nginx的worker_processes和

worker_connections进行调整,来达到性能的调优。

如果觉得《Nginx + Tomcat 负载均衡集群配置》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。