开发的应用采用F5负载均衡交换机,F5将请求转发给5台hp unix服务器,每台服务器有多个webserver实例,对外提供web服务和socket等接口服务。之初,曾有个小小的疑问为何不采用开源的apache、Nginx软件负载,F5设备动辄几十万,价格昂贵?自己一个比较幼稚的问题,后续明白:F5是操作于IOS网络模型的传输层,Nginx、apache是基于http反向代理方式,位于ISO模型的第七层应用层。直白些就是TCP UDP 和http协议的区别,Nginx不能为基于TCP协议的应用提供负载均衡。

了解了二者之间的区别于应用场景,对Nginx产生浓厚的兴趣,阅读张宴的<实战Nginx>(这个85年的小伙子年轻有为羡慕+妒忌),搞明白了大致原理和配置,centos 下对Nginx+tomcat负载均衡做了配置尝试,将全部请求转发到tomcat,并未做静态,动态分开,图片防盗链等配置。

  

Nginx 介绍

Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。

      上面的全是Nginx介绍基本上是废话,下面转入正题,图文结合展示基本配置,首先是window环境、其次是Ubuntu环境(Vbox虚拟)。本文主要基于Nginx下配置两台tomcat,结构如下图:

centos6.3 base64环境:Nginx+Tomcat7

1、下载地址

http://nginx.org/en/download.html ,这里我们推荐下载稳定版(stable versions),本文采用nginx-1.44。

2、目录结构

 

Nginx-

|_  conf   配置目录

|_  contrib

|_  docs 文档目录

|_  logs  日志目录

|_  temp 临时文件目录

|_  html 静态页面目录

|_  nginx.exe 主程序

centos 下安装Nginx极其简单,解压缩到一个无空格的英文目录即可(个人习惯,担心中文出问题), 使用/alidata/server/nginx/sbin/nginx -s reload启动,这里我安装到:/alidata/server/目录,下面涉及到的tomcat也安装在此目录

# /alidata/server/nginx/sbin/nginx -s reload

如果想要停止

# /alidata/server/nginx/sbin/nginx -s stop

3、nginx.conf配置

  nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在/alidata/server/、默认主配置文件为/alidata/server/nginx/conf/。下面是nginx作为前端反向代理服务器的配置。

#Nginx所用用户和组
user www www; #工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes ; #错误日志存放路径
#error_log /alidata/log/nginx/error.log crit;
#error_log /alidata/log/nginx/error.log notice;
error_log /alidata/log/nginx/error.log info; #指定pid存放文件
pid /alidata/server/nginx/logs/nginx.pid; events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
use epoll; #允许最大连接数
worker_connections ;
} 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 off;
access_log logs/access.log; client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m; client_header_buffer_size 1k;
large_client_header_buffers 4k; sendfile on;
tcp_nopush on;
tcp_nodelay on; #keepalive_timeout ; include gzip.conf; upstream localhost {
#ip_hash
#ip_hash;
server localhost:;
server localhost:;
} server {
listen ;
server_name localhost; location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
root /alidata/www/static;
expires 24h;
}
location / {
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_pass http://localhost;
} }
}

代理设置如下:
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 ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_buffer_size 4k;
proxy_buffers 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

gzip压缩相关配置如下:

gzip.conf代码
gzip on;
gzip_min_length ;
gzip_types text/plain text/css application/x-javascript;

4、Tomcat配置

  对于tomcat大家都很熟悉,只需要修改server.xml配置文件即可,这里我们以apache-tomcat-7.0.47为例,分别在server目录,解压缩并命名为:apache-tomcat-7.0.47-cluster-1、apache-tomcat-7.0.47-cluster-2。

第一处端口修改:

<!--  修改port端口:18006 俩个tomcat不能重复,端口随意,别太小-->
<Server port="18006" shutdown="SHUTDOWN">

第二处端口修改:

<!-- port="18081" tomcat监听端口,随意设置,别太小 -->
<Connector port="9091" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

第三处端口修改:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  

Engine元素增加jvmRoute属性:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  

--两个tomcat的端口别重复,保证能启动起来,另一个tomcat配置省略,监听端口为9091,

改进后的nginx 配置文件

#user  nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /alidata/www/static/log/nginx/log.log info;
#pid logs/nginx.pid; events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
use epoll; #允许最大连接数
worker_connections ;
} 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_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 1k;
large_client_header_buffers 4k; sendfile on;
tcp_nopush on;
tcp_nodelay on; #tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; include gzip.conf; upstream localhost { #ip_hash;
server localhost:;
server localhost:;
} server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ~ \.(jsp|do|cy|)$ {
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
chunked_transfer_encoding off;
} location / {
root /alidata/www/static/;
index index.html index.htm;
expires 24h;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

---------------------------------------------------------------------- over ------------------------------------------------------------------

nginx + tomcat集群和动静资源分离的更多相关文章

  1. nginx+ tomcat集群+动静资源分离

    不知道为什么这个随便删不掉,写了也值显示一半一半不显示, 我把重新写了一遍: nginx + tomcat集群和动静资源分离

  2. 【nginx+tomcat集群】Nginx1.12.2+Tomcat7集群+负载均衡+Session共享

    今天想着将项目优化一下,就想的实现集群分布,在本机测试:利用nginx+tomcat实现 通过上一篇博客(http://www.cnblogs.com/qlqwjy/p/8535235.html),N ...

  3. nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路

    前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...

  4. nginx+tomcat集群配置(1)---根目录设定和多后端分发配置

    前言: 对于javaer而言, nginx+tomcat集群配置, 已然成了web应用部署的主流. 大公司如此, 小公司亦然. 对于个人开发者而言, 资源有限, 往往多个web应用混部于一台服务器(云 ...

  5. Nginx+Tomcat集群配置

    Nginx+Tomcat集群配置 一台虚拟机作为Nginx服务 两太虚拟机配置Tomcat+jdk环境 Nginx测试 启动: cd usr/local/nginx/sbin ./nginx ---& ...

  6. 160513、nginx+tomcat集群+session共享(linux)

    第一步:linux中多个tomcat安装和jdk安装(略) 第二步:nginx安装,linux中安装nginx和windows上有点不同也容易出错,需要编译,这里做介绍 一.安装依赖 gcc open ...

  7. Nginx+Tomcat集群+session共享

    Nginx+Tomcat集群+session共享 1)安装Nginx 2)配置多个Tomcat,在server.xml中修改端口(端口不出现冲突即可) 3)在nginx.conf文件中配置负载均衡池, ...

  8. nginx+tomcat集群方法

    下载地址:wget http://nginx.org/download/nginx-1.16.1.tar.gz 解压:tar -zxvf 预编译 nginx+tomcat集群方法: 进入nginx配置 ...

  9. nginx+tomcat集群负载均衡(实现session复制)

    转自:http://talangniao.iteye.com/blog/341512 架构描述 前端一台nginx服务器做负载均衡器,后端放N台tomcat组成集群处理服务,通过nginx转发到后面( ...

随机推荐

  1. iOS6定位服务编程详解

    现在的移动设备很多都提供定位服务,使用iOS系统的iPhone.iPod Touch和iPad都可以提供位置服务,iOS设备能提供3种不同途径进行定位:Wifi, 蜂窝式移动电话基站, GPS卫星 i ...

  2. 分栏控制器和导航栏目tabBarItem,UINavigationController

    ////  AppDelegate.m//  TabBarControllerDemo////  Created by qianfeng on 15/9/22.//  Copyright (c) 20 ...

  3. Xcode6 模拟器不显示键盘

    在学习加法计算器时,程序运行后发现点击模拟器上的输入框时有时候键盘可以弹出来,有时候又弹不出来. 网上查询结果只需要在模拟器的菜单中找到hardware -> keyboard -> 取消 ...

  4. 20150222—LINQ to SQL 插入、更新和删除

    注意,使用LINQ to SQL时, 表中必须有一个主键才可以起效,否则系统将无法对数据作出修改 插入新数据,根据上一片的文章实例在其中添加新的控件: 编号TextBox(Name):sno 名字Te ...

  5. 再谈移动端Web屏幕适配

    一个多月前水了一篇移动web屏幕适配方案,当时噼里啪啦的写了一通,自我感觉甚是良好.不过最近又有一些新的想法,和之前的有一些不同. 先说一下淘宝的方案,感觉现在好多的适配方案都是受了它的影响,上周六看 ...

  6. JavaScript学习笔记 -- 带参数arguments的函数的用法

    JavaScript函数有带参数与不带参数两种形式,不带参数情况如下: function myFunction() { alert('HelloWorld!') } 在这种类型的函数中,输出值是确定的 ...

  7. 济南学习 Day 2 T3 am

    [问题描述]m× m的方阵上有n棵葱,你要修一些栅栏把它们围起来.一个栅栏是一段沿着网格建造的封闭图形(即要围成一圈) .各个栅栏之间应该不相交.不重叠且互相不包含.如果你最多修k个栅栏,那么所有栅栏 ...

  8. Template_5模板拾遗1

    1,typename和class模板参数作为类的时候只能用classtemplate<typename T, template<typename ELEM> class CONT = ...

  9. atoi 实现

    int atoi(const char *nptr); 把字符串转换成整型数.ASCII to integer 的缩写. 头文件: #include <stdlib.h> 参数nptr字符 ...

  10. iOS 系统架构及常用框架

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...