搭建nginx+tomcat+Java的负载均衡环境
转载 未测
供参考
另外这篇文章也不错。http://blog.csdn.net/wang379275614/article/details/47778201
一、简介:
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]
- #运行用户
- #user nobody;
- #开启进程数 <=CPU数
- worker_processes 1;
- #错误日志保存位置
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #进程号保存文件
- #pid logs/nginx.pid;
- #等待事件
- events {
- #Linux下打开提高性能
- #use epoll;
- #每个进程最大连接数(最大连接=连接数x进程数)
- worker_connections 1024;
- }
- http {
- #文件扩展名与文件类型映射表
- include mime.types;
- #默认文件类型
- default_type application/octet-stream;
- #日志文件输出格式 这个位置相于全局设置
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #请求日志保存位置
- #access_log logs/access.log main;
- #设定请求缓冲
- client_header_buffer_size 1k;
- large_client_header_buffers 4 4k;
- #打开发送文件
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #客户端上传文件大小控制
- client_max_body_size 8m;
- #打开gzip压缩
- #gzip on;
- #设定负载均衡的服务器列表
- #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 {
- #监听IP端口
- listen 80;
- #主机名
- server_name localhost;
- #root
- #设置字符集
- #charset koi8-r;
- #本虚拟server的访问日志 相当于局部变量
- #access_log logs/host.access.log main;
- #日志文件输出格式
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- location / {
- root html;
- index index.html index.htm;
- }
- #静态文件缓存时间设置
- #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
- # expires 30d;
- #}
- #静态文件缓存时间设置
- #location ~ .*\.(js|css)?${
- # expires 1h;
- #}
- #对本server"/"启用负载均衡
- #location / {
- # proxy_pass http://mysvr;
- # 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_buffer_size 4k;
- # proxy_buffers 4 32k;
- # proxy_busy_buffers_size 64k;
- # proxy_temp_file_write_size 64k;
- #}
- #设定查看Nginx状态的地址
- #location /NginxStatus {
- # stub_status on;
- # access_log on;
- # auth_basic “NginxStatus”;
- # auth_basic_user_file conf/htpasswd;
- #}
- #error_page 404 /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_index index.php;
- # fastcgi_param SCRIPT_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 {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- server {
- #多监听
- listen localhost:8666;
- #主机名
- server_name LIULJ2576;
- #WEB文件路径
- root E:/Portal;
- #默认首页
- index HomePage.html;
- #location / {
- # #这里相当于局部变量
- # root E:/Portal;
- # index HomePage.html;
- #}
- }
- # HTTPS server HTTPS SSL加密服务器
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
#号为注释内容,我们在cmd下运行nginx
启动成功,出错的话,可以查询日志(日志路径是配置文件指定的,你可以修改存到其它位置)
访问一下第二个server 配置的localhost:8666地址,结果出现
三、Nginx可以通过以下两种方式来实现与Tomcat的耦合:
将静态页面请求交给Nginx,动态请求交给后端Tomcat处理。
将所有请求都交给后端的Tomcat服务器处理,同时利用Nginx自身的负载均衡功能进行多台Tomcat服务器的负载均衡。
下面通过两个配置实例分别讲述这两种实现
一、动态页面和静态页面分离的实例
这里假定Tomcat服务器的IP地址为192.168.12.130,同时Tomcat服务器开放的服务器端口为8080。Nginx相关配置代码如下:
- server {
- listen 80;
- server_name www.ixdba.net;
- root /web/www/html;
- location /img/ {
- alias /web/www/html/img/;
- }
- location ~ (\.jsp)|(\.do)$ {
- proxy_pass http://192.168.12.130:8080;
- 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_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- }
- }
在这个实例中,首先定义了一个虚拟主机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
|
upstream mytomcats { server 192.168 . 12.131 : 8000 ; server 192.168 . 12.132 : 8080 ; server 192.168 . 12.133 : 8090 ; } server { listen 80 ; server_name www.ixdba.net; location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { root /web/www/html/; } location / { proxy_pass http: //mytomcats; 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_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; |
搭建nginx+tomcat+Java的负载均衡环境的更多相关文章
- 【转】搭建nginx+tomcat+Java的负载均衡环境
一.简介: Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱.虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很 ...
- Nginx+Tomcat+Redis实现负载均衡、资源分离、session共享
Nginx+Tomcat+Redis实现负载均衡.资源分离.session共享 CentOS安装Nginx http://centoscn.com/CentosServer/www/2013/0910 ...
- keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)
keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.
- [转]搭建Keepalived+Nginx+Tomcat高可用负载均衡架构
[原文]https://www.toutiao.com/i6591714650205716996/ 一.概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最 ...
- Nginx+Tomcat+Memcache实现负载均衡及Session共享
第一部分 环境介绍 部署环境: Host1:Nginx.Memcached.Tomcat1 Host2:Tomcat2 Tomcat_version:8.0.38 第二部分 Nginx+Tomcat实 ...
- Keepalived + Nginx + Tomcat 高可用负载均衡架构
环境: 1.centos7.3 2.虚拟ip:192.168.217.200 3.192.168.217.11.192.168.217.12上分别部署Nginx Keepalived Tomcat并进 ...
- Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术
一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判 ...
- LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)
一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Red ...
- Nginx+Tomcat+Https 服务器负载均衡配置
这篇过气了! 重新补一个:http://www.cnblogs.com/hackyo/p/6809773.html 由于需要,得搭建个nginx+tomcat+https的服务器,搜了搜网上的发现总是 ...
随机推荐
- 关于xfce桌面程序启动失败
当双击桌面图标的时候,出现如下错误信息:Process org.xfce.FileManager exited with status 1 于是做出如下尝试: 1. ps aux | grep Fil ...
- 货运APP产品魔力=卓越功能×情感诉求
货运APP产品魔力=卓越功能×情感诉求 [导语]在滴滴打车.快的打车软件风靡全国之际,很多企业也在探索如何将其应用到公路货运领域用以整合社会运力资源.但一方面大多数人聚焦的是干线运输中的长途货车运 ...
- 利用 async & await 的异步编程
走进异步编程的世界 - 开始接触 async/await 利用 async & await 的异步编程 async 的三大返回类型 公司技术需求备忘录
- 省市联动sql脚本
create database ProCityData use Procitydata --创建Province(省表) create table Province ( ProID int prima ...
- module not exists: xxxx Error in ThinkPHP
Introduction ThinkPHP is a very charming PHP framework to easily build a website. It's structured in ...
- Time crumbles things; everything grows old under the power of Time and is forgotten through the lapse of Time
Time crumbles things; everything grows old under the power of Time and is forgotten through the laps ...
- 【转】eclipse 创建struts2
转载地址:http://blog.163.com/sean_zwx/blog/static/169032755201091595558378/ 今天我用Ecipse搭建Struts框架,并且使用Str ...
- ubuntu16.04下配置JDK 1.8+安装Java EE,并实现最大子数组算法
软工第二次作业: 1.在个人电脑中安装一个集成开发环境(Microsoft Visual Studio.Eclipse或其它工具均可),要求该环境能够提供单元自动测试功能: 2.记录安装过程,并将全部 ...
- [转]SqlSever2005 一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】一周搞定
对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感 谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当 ...
- linux 调用java main方法
#!/bin/shexport LANG=zh_CNtimestamp=`date +%Y%m%d%H%M`/opt/java6/bin/java -Xms128m -Xmx512m -Dfile.e ...