Nginx+Tomcat7+Memcached负载均衡集群+Session共享


Nginx+Tomcat7+Memcached负载均衡集群服务搭建

操作系统:CentOS6.5

本文档主要讲解,如何在CentOS6.5下搭建Nginx+Tomcat7+Memcached负载均衡集群服务器,Nginx负责负载均衡,Tomcat7负责实际服务,Memcached负责同步Tomcat7的Session,达到Session共享的目的。

相关文件下载:

------------------------------------------分割线------------------------------------------

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2015年资料/1月/17日/Nginx+Tomcat7+Memcached负载均衡集群+Session共享

下载方法见

------------------------------------------分割线------------------------------------------

1.安装Nginx
Nginx官网:http://nginx.org/
下载最新稳定版本。在安装Nginx之前,需要先安装gcc、 openssl、 pcre和zlib软件库。

1.1安装gcc、gcc-c++
安装命令:
#yum install gcc
#yum install gcc-c++

1.2安装openssl
openssl官网:http://www.openssl.org/
安装版本:openssl-1.0.1i.tar.gz
安装命令:
#tar -zxvf openssl-1.0.1i.tar.gz
#cd openssl-1.0.1i
#./config --prefix=/usr/local/openssl-1.0.1i    #prefix指定安装目录
#make
#make install
【注意】:此处使用的是config命令,而不是平常的configure命令
安装完成后,到/usr/local/下查看是否安装成功。如果安装出错,需要重新加压缩,重新安装。

1.3安装pcre
pcre官网:http://www.pcre.org/
安装版本:pcre-8.35.tar.gz
安装命令:
#tar -zxvf pcre-8.35.tar.gz
#cd pcre-8.35
#./configure --prefix=/usr/local/pcre-8.35    #prefix指定安装目录
#make
#make install
安装完成后,到/usr/local/下查看是否安装成功。如果安装出错,需要重新加压缩,重新安装。

【注意】:如果没有安装c++编译器,这个软件的安装会报错!

1.4安装zlib
zlib官网:http://www.zlib.net/
安装版本:zlib-1.2.8.tar.gz
安装命令:
#tar -zxvf zlib-1.2.8.tar.gz
#cd zlib-1.2.8
#./configure --prefix=/usr/local/zlib-1.2.8    #prefix指定安装目录
#make
#make install
安装完成后,到/usr/local/下查看是否安装成功。如果安装出错,需要重新加压缩,重新安装。

1.5安装Nginx
安装版本:nginx-1.6.1.tar.gz
安装命令:
#tar -zxvf nginx-1.6.1.tar.gz
#cd nginx-1.6.1
#./configure
--prefix=/usr/local/nginx-1.6.1                #prefix指定安装目录
--with-openssl=/home/zht/src/openssl-1.0.1i    #指的是openssl源码路径
--with-pcre=/home/zht/src/pcre-8.3.5          #指的是pcre的源码路径
--with-zlib=/home/zht/src/zlib-1.2.8          #指的是zlib 的源码路径
--with-http_ssl_module
#make
#make install
安装完成后,到/usr/local/下查看是否安装成功。如果安装出错,需要重新加压缩,重新安装。
 
1.5.1 配置Nginx
配置文件目录:/usr/local/nginx-1.6.1/conf/nginx.conf
#cd /usr/local/nginx-1.6.1/conf
#vi nginx.conf

【修改后的配置文件如下】:
#创建进程的用户和用户组
user    feng  feng;

#useradd feng


#服务进程数量,一般等于CPU数量
worker_processes 1;

#全局错误日志定义,建议开启error级别日志.[ debug | info | notice | warn | error | crit ]
error_log logs/error.log error;
#error_log logs/error.log  notice;
#error_log logs/error.log  info;

#记录进程ID的文件
#pid      logs/nginx.pid;
events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能.Linux建议使用epoll,FreeBSD建议使用kqueue.
    useepoll;
    #一个worker_processe允许的最近并发连接数量
    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;

    sendfile        on;
    #tcp_nopush    on;

    #http连接的持续时间
    keepalive_timeout  65;

    #gzip压缩设置
    gzip  on;          #开启gzip
    gzip_min_length 1k;  #最小压缩文件大小
    gzip_buffers 4 16k;  #压缩缓冲区
    #http的协议版本(1.0/1.1),默认1.1,前端如果是squid2.5请使用1.0
    gzip_http_version 1.1;
    #gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
    gzip_comp_level 2;   
    #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
    gzip_varyon;
    #gzip压缩类型,不用添加text/html,否则会有警告信息
    gzip_types text/plain text/javascript text/css application/xmlapplication/x-javascript application/json;

    #设定负载均衡的服务器列表,可以设置多个upstream,但mysvr名字要区分
    upstreammy ClusterServer1 {
    #weigth参数表示权值,权值越高被分配到的几率越大
      server 192.168.10.100:8080  weight=5;
      server 192.168.10.101:8080  weight=5;
      server 192.168.10.102:8080  weight=5;
    }

    server {
      #nginx监听的端口号
      listen      80;
      #域名可以有多个,用空格隔开
      server_name  192.168.10.222;

      #字符编码方式
      charset utf-8;

      #设定本虚拟主机的访问日志。关闭日志可以减少IO,提高性能。
      #access_log logs/host.access.log  main;

      #默认请求  此处也可对网站进行动静分离的配置
      location / {
          #定义服务器的默认网站根目录位置
          root  html;
          #定义首页索引文件的名称
          index  index.html index.htmindex.jsp;
          #请求转向mysvr 定义的服务器列表
          proxy_pass    http://myClusterServer1;
          proxy_redirect default;
            #跟代理服务器连接的超时时间,必须留意这个time out时间不能超过75秒,当一台服务器当掉时,过10秒转发到另外一台服务器。
          proxy_connect_timeout 10;
        }

      #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;
      #}
    }


    # anothervirtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen      8000;
    #    listen      somename:8080;
    #    server_name somename  alias  another.alias;

    #    location / {
    #        root  html;
    #        index index.html index.htm;
    #    }
    #}


    # HTTPSserver
    #
    #server {
    #    listen      443 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;
    #    }
    #}

}
1.5.2启动与关闭Nginx
(1)启动
#/usr/local/nginx-1.6.1/sbin/nginx
确保系统的 80 端口没被其他程序占用


重新加载配置文件:
#/usr/local/nginx-1.6.1/sbin/nginx  -s  reload

(2)关闭:
#pkill nginx

(3)检查是否启动成功:
#netstat -ano | grep80 有结果输入说明启动成功

打开浏览器访问此机器的 IP,如果浏览器出现Welcome to nginx! 则表示 Nginx 已经安装并运行成功。如果已经配置了负载均衡服务器,则会看Tomcat中的网站页面。

--------------------------------------分割线 --------------------------------------

CentOS 6.2实战部署Nginx+MySQL+PHP

使用Nginx搭建WEB服务器

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

CentOS 6.3下Nginx性能调优

CentOS 6.3下配置Nginx加载ngx_pagespeed模块

CentOS 6.4安装配置Nginx+Pcre+php-fpm

Nginx安装配置使用详细笔记

Nginx日志过滤 使用ngx_log_if不记录特定日志

--------------------------------------分割线 --------------------------------------

更多详情见请继续阅读下一页的精彩内容:

  • 1
  • 2
  • 下一页

相关内容

    暂无相关文章