您的位置:首页 > 运维架构 > Tomcat

搭建nginx+tomcat+Java的负载均衡环境

2016-02-03 00:00 736 查看
一、简介:
Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱。虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多。
二、下载安装:获取【下载地址】
下载nginx
http://nginx.org/en/download.html
下载解压后放到C:\nginx-1.0.4(官网这样要求的,不知道放其它盘有没有问题)
启动nginx.exe,然后在浏览器输入127.0.0.1即可
配置自己的项目测试
第二环节我们使用了默认的nginx.conf。Nginx的配置文件都存于目录conf文件下,其中nginx.conf是它的主配置文件。
以下为我加上注释并配置的新的虚拟server]
Java代码

#运行用户

#usernobody;

#开启进程数<=CPU数

worker_processes1;

#错误日志保存位置

#error_loglogs/error.log;

#error_loglogs/error.lognotice;

#error_loglogs/error.loginfo;

#进程号保存文件

#pidlogs/nginx.pid;

#等待事件

events{

#Linux下打开提高性能

#useepoll;

#每个进程最大连接数(最大连接=连接数x进程数)

worker_connections1024;

}

http{

#文件扩展名与文件类型映射表

includemime.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"';

#请求日志保存位置

#access_loglogs/access.logmain;

#设定请求缓冲

client_header_buffer_size1k;

large_client_header_buffers44k;

#打开发送文件

sendfileon;

#tcp_nopushon;

#keepalive_timeout0;

keepalive_timeout65;

#客户端上传文件大小控制

client_max_body_size8m;

#打开gzip压缩

#gzipon;

#设定负载均衡的服务器列表

#upstreammysvr{

##weigth参数表示权值,权值越高被分配到的几率越大

##本机上的Squid开启3128端口

##server192.168.8.1:3128weight=5;

##server192.168.8.2:80weight=1;

##server192.168.8.3:80weight=6;

#}

#第一个虚拟主机

server{

#监听IP端口

listen80;

#主机名

server_namelocalhost;

#root

#设置字符集

#charsetkoi8-r;

#本虚拟server的访问日志相当于局部变量

#access_loglogs/host.access.logmain;

#日志文件输出格式

#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'

#'$status$body_bytes_sent"$http_referer"'

#'"$http_user_agent""$http_x_forwarded_for"';

location/{

roothtml;

indexindex.htmlindex.htm;

}

#静态文件缓存时间设置

#location~.*\.(gif|jpg|jpeg|png|bmp|swf)${

#expires30d;

#}

#静态文件缓存时间设置

#location~.*\.(js|css)?${

#expires1h;

#}

#对本server"/"启用负载均衡

#location/{

#proxy_passhttp://mysvr;
#proxy_redirectoff;

#proxy_set_headerHost$host;

#proxy_set_headerX-Real-IP$remote_addr;

#proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

#client_max_body_size10m;

#client_body_buffer_size128k;

#proxy_connect_timeout90;

#proxy_send_timeout90;

#proxy_read_timeout90;

#proxy_buffer_size4k;

#proxy_buffers432k;

#proxy_busy_buffers_size64k;

#proxy_temp_file_write_size64k;

 
7fe0
;#}

#设定查看Nginx状态的地址

#location/NginxStatus{

#stub_statuson;

#access_logon;

#auth_basic“NginxStatus”;

#auth_basic_user_fileconf/htpasswd;

#}

#error_page404/404.html;

#redirectservererrorpagestothestaticpage/50x.html

#

error_page500502503504/50x.html;

location=/50x.html{

roothtml;

}

#proxythePHPscriptstoApachelisteningon127.0.0.1:80

#

#location~\.php${

#proxy_passhttp://127.0.0.1;
#}

#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000

#

#location~\.php${

#roothtml;

#fastcgi_pass127.0.0.1:9000;

#fastcgi_indexindex.php;

#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;

#includefastcgi_params;

#}

#denyaccessto.htaccessfiles,ifApache'sdocumentroot

#concurswithnginx'sone

#

#location~/\.ht{

#denyall;

#}

}

#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration

server{

#多监听

listenlocalhost:8666;

#主机名

server_nameLIULJ2576;

#WEB文件路径

rootE:/Portal;

#默认首页

indexHomePage.html;

#location/{

##这里相当于局部变量

#rootE:/Portal;

#indexHomePage.html;

#}

}

#HTTPSserverHTTPSSSL加密服务器

#

#server{

#listen443;

#server_namelocalhost;

#sslon;

#ssl_certificatecert.pem;

#ssl_certificate_keycert.key;

#ssl_session_timeout5m;

#ssl_protocolsSSLv2SSLv3TLSv1;

#ssl_ciphersALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

#ssl_prefer_server_cipherson;

#location/{

#roothtml;

#indexindex.htmlindex.htm;

#}

#}

}

#号为注释内容,我们在cmd下运行nginx
启动成功,出错的话,可以查询日志(日志路径是配置文件指定的,你可以修改存到其它位置)
访问一下第二个server配置的localhost:8666地址,结果出现

三、Nginx可以通过以下两种方式来实现与Tomcat的耦合:
将静态页面请求交给Nginx,动态请求交给后端Tomcat处理。
将所有请求都交给后端的Tomcat服务器处理,同时利用Nginx自身的负载均衡功能进行多台Tomcat服务器的负载均衡。
下面通过两个配置实例分别讲述这两种实现
一、动态页面和静态页面分离的实例
这里假定Tomcat服务器的IP地址为192.168.12.130,同时Tomcat服务器开放的服务器端口为8080。Nginx相关配置代码如下:
Java代码

server{

listen80;

server_namewww.ixdba.net;

root/web/www/html;

location/img/{

alias/web/www/html/img/;

}

location~(\.jsp)|(\.do)${

proxy_passhttp://192.168.12.130:8080;
proxy_redirectoff;

proxy_set_headerHost$host;

proxy_set_headerX-Real-IP$remote_addr;

proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

client_max_body_size10m;

client_body_buffer_size128k;

proxy_connect_timeout90;

proxy_send_timeout90;

proxy_read_timeout90;

proxy_buffer_size4k;

proxy_buffers432k;

proxy_busy_buffers_size64k;

proxy_temp_file_write_size64k;

}

}

在这个实例中,首先定义了一个虚拟主机www.ixdba.net,然后通过location指令将/web/www/html/img/目录下的静态文件交给Nginx来完成。最后一个location指令将所有以.jsp、.do结尾的文件都交给Tomcat服务器的8080端口来处理,即http://192.168.12.130:8080。
需要特别注意的是,在location指令中使用正则表达式后,proxy_pass后面的代理路径不能含有地址链接,也就是不能写成http://192.168.12.130:8080/,或者类似http://192.168.12.130:8080/jsp的形式。在location指令不使用正则表达式时,没有此限制。
2、多个tomcat负载均衡的实例
这里假定有3台Tomcat服务器,分别开放不同的端口,地址如下:
?

1

2

3
192.168
.
12.131
:
8000

192.168
.
12.132
:
8080

192.168
.
12.133
:
8090
Nginx的相关配置代码如下:
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
upstreammytomcats{


server
192.168
.
12.131
:
8000
;


server
192.168
.
12.132
:
8080
;


server
192.168
.
12.133
:
8090
;

}



server{


listen
80
;


server_namewww.ixdba.net;



location~*\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)${


root/web/www/html/;

}



location/{


proxy_passhttp:
//mytomcats;


proxy_redirectoff;


proxy_set_headerHost$host;


proxy_set_headerX-Real-IP$remote_addr;


proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;


client_max_body_size10m;


client_body_buffer_size128k;


proxy_connect_timeout
90
;


proxy_send_timeout
90
;


proxy_read_timeout
90
;


proxy_buffer_size4k;


proxy_buffers
4
32k;


proxy_busy_buffers_size64k;


proxy_temp_file_write_size64k;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx