开发的应用采用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. Process Stats:了解你的APP如何使用内存(转)

    原文地址:http://android-developers.blogspot.com/2014/01/process-stats-understanding-how-your.html?m=1 原作 ...

  2. (转)RabbitMQ消息队列(五):Routing 消息路由

    上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings绑定 上 ...

  3. hdu 2057 A+B Again

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2057 题目分析:涉及到16进制内的加法,可以用%I64x直接来处理,要注意到16进制中负数是用补码来表 ...

  4. 【风马一族_Android】Android Studio 给APP设置签名

    在Android Studio中,给App签名,如果没有给App设置签名的话,Android Studio会主动给app设置一个默认的签名 接下来,介绍主动给App设置一个签名的整个步骤过程: 1) ...

  5. arduino入门学习实现语音控制LED灯

    需要的准备的硬件arduino+PC+麦克风实现语音命令控制LED灯的亮灭. 首先需要将写好的arduino程序烧录到arduino uno主板中,下面是代码如下: int val;//定义变量val ...

  6. 【Qt】QSettings读写注册表、配置文件【转】

    简述 一般情况下,我们在开发软件过程中,都会缓存一些信息到本地,可以使用轻量级数据库sqlite,也可以操作注册表.读写配置文件. 关于QSettings的使用前面已经介绍过了,比较详细,见“更多参考 ...

  7. 使用socket.io搭建聊天室

    最近在学习nodejs,需要找一些项目练练手.找来找去发现了一个聊天室的教程,足够简单,也能从中学到一些东西.下面记录我练习过程中待一些笔记. nodeJS模块 共用到了2个模块,express和so ...

  8. php curl post

    public static function curlPost($url, $data=array()) {        $ch = curl_init();        if (is_array ...

  9. 从java到php

    从大一开始就开始接触java这门语言.但是在这之前也接触了vb,c,asp等等开发语言,唯独java让人感觉严谨,清爽,各种数据之间的不拖泥带水.这才花费更多时间去研究他.但是学着学着,发现他体系的庞 ...

  10. 分享:perl 文件操作总结

    发布:thebaby 来源:net [大 中 小] perl 文件操作,包括打开.关闭文件,读取.定入文件等.原文链接:http://www.jbxue.com/article/3153.html 打 ...