开发的应用采用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. windows使用技巧

    使用命令行工具(cmd)并总是要切换到同一个文件夹   ◆通常的办法 打开命令行工具 切换到指定盘符 切换到目录 Microsoft Windows [Version 10.0.14393] (c) ...

  2. 最新32位和64位xp,win7,win8,win10系统大全(电脑装机版)

    一.系统主要特点 1.安装维护方便快速 - 全自动无人值守安装,采用万能GHOST技术,安装系统过程只需3-5分钟,适 合新旧各种机型. - 集成常见硬件驱动,智能识别+预解压技术,绝大多数硬件可以快 ...

  3. Mysql中使用FIND_IN_SET解决IN条件为字符串时只有第一个数据可用的问题

    今天在使用Mysql的存储过程处理数据的批量删除时,遇到了WHERE条件中使用IN(strlist)时(strlist为逗号分隔的字符串),只有strlist的第一个元素才有效的问题,现在将问题和解决 ...

  4. 【ASP.NET】获取网站目录的方法

         获取网站物理路径: HttpRuntime.AppDomainAppPath 获取网站虚拟路径: HttpRuntime.AppDomainAppVirtualPath

  5. Myeclipse安装svn插件(link方式)

    Myeclipse安装svn插件(link方式) 优点:1.离线安装2.非侵入式 演示版本 myeclipse--myeclipse8.6 svn--subeclipse-site-1.6.5.zip ...

  6. NOIP 2015普及组复赛Day1 T1 == Codevs4510 神奇的幻方

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold  题目描述 Description: 幻方是一种很神奇的N∗N矩阵:它由数字 1,2,3, … … ,N∗N构成, ...

  7. OpenGL第15,16,17讲小结

    这三讲没有什么特别的感觉,15讲把纹理贴到3D字体上,16讲讲了雾的生成,17讲是通过以事先保存好的纹理贴图的字体来显示2D文字,这样子不用调用windows的字体,而是使用纹理中的字体.这样也在一定 ...

  8. javaScript 连续子数列最大和

    <!DOCTYPE html> <html> <head> <title></title> <meta charset=utf-8&g ...

  9. javascript代码复用--继承

    由于javascript没有类的概念,因此无法通过接口继承,只能通过实现继承.实现继承是继承实际的方法,javascript中主要是依靠原型链要实现. 原型链继承 原型链继承是基本的继承模式,其本质是 ...

  10. Js apply() call()使用详解

    Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里 ...