Nginx搭建Tomcat集群【入门】

准备三台虚拟机 两台也可 我准备的是3台centos

192.168.138.132  --ngnix负载均衡主机
192.168.138.133  --
192.168.138.134  -- 

1.关闭三台机器的 防火墙

关闭firewall:
systemctl stop firewalld.service       #停止firewall
systemctl disable firewalld.service    #禁止firewall开机启动
firewall-cmd --state                         #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

2.然后检查一下 负载均衡主机能不能ping通其他两台主机

3.导入外部软件库

    1. rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm
    2. rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm
    3. rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

4 .Nginx下载

    1)pcre
        http://www.pcre.org/
    2)nginx
        http://nginx.org/
        http://download.csdn.net/download/qq_32351227/10123762

5. Nginx安装

5.1 安装前的准备

    1)准备 pcre-8.12.tar.gz。该文件为正则表达式库。让nginx支持rewrite需要安装这个库。
    2) 准备 nginx-1.5.0.tar.gz。该文件为nginx的linux版本安装文件。
    3)确保进行了安装了linux常用必备支持库。
    在CentOS安装软件的时候,可能缺少一部分支持库,而报错。这里首先安装系统常用的支持库。那么在安装的时候就会减少很多的错误的出现。
    yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel

5.2 正则表达式库安装

1)确保进行了安装了linux常用必备支持库。。需要安装g++、gcc。
    yum install gcc-c++

2) 上传pcre-8.12.tar.gz, nginx-1.5.0.tar.gz 
    到 /usr/softs/nginx目录下

3)解压pcre-8.12.tar.gz
    tar -zvxf pcre-8.12.tar.gz
    # cd /softs/nginx
    # tar -zxvf pcre-8.12.tar.gz

4)进入解压后的目录
    # cd pcre-8.12

5)配置
    #  ./configure

6) 编译
    #  make

7) 安装
    #  make install

5.3 Nginx安装

1) 创建用户nginx使用的www用户。
    # groupadd  nginx    #添加nginx组    
    # useradd -g  nginx nginx -s /bin/false    #创建nginx运行账户nginx并加入到nginx组,不允许nginx用户直接登录系统



2) 判断系统是否安装了zlib-devel。如果没有安装。使用
    # yum install -y zlib-devel

3) 解压
    # cd  /usr/softs/ 
    # tar -zxvf nginx-1.5.0.tar.gz  
    mv nginx-1.5.0 nginx #重命名

4) 进入目录
   cd  /usr/softs/nginx
   mkdir logs #创建logs 目录


5) 配置。我们将软件安装在/usr/softs/nginx目录下。
    #   ./configure --user=nginx --group=nginx --prefix=/usr/softs/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

6)编译
    # make

7)  安装
    #  make install


8)  检查是否安装成功
    usr/softs/nginx/sbin/nginx -t


9)成功结果显示:
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6.启停nginx

 usr/softs/nginx/sbin/nginx  #启动
 usr/softs/nginx/sbin/nginx -s reload #重新加载配置文件
 usr/softs/nginx/sbin/nginx -s stop #停止
 打开  192.168.138.132 显示welcome to nginx 就是成功了

7.修改配置文件:

conf/nginx.conf

user nginx;
worker_processes  1;
pid logs/nginx.pid;   

worker_rlimit_nofile 102400;   
events   
{   
    use epoll;   
    worker_connections 102400;   
}   
http   
{   
  include       mime.types;   
  default_type  application/octet-stream;   
  fastcgi_intercept_errors on;   
  charset  utf-8;   
  server_names_hash_bucket_size 128;   
  client_header_buffer_size 4k;   
  large_client_header_buffers 4 32k;   
  client_max_body_size 300m;   
  sendfile on;   
  tcp_nopush     on;   

  keepalive_timeout 60;   

  tcp_nodelay on;   
  client_body_buffer_size  512k;   

  proxy_connect_timeout    5;   
  proxy_read_timeout       60;   
  proxy_send_timeout       5;   
  proxy_buffer_size        16k;   
  proxy_buffers            4 64k;   
  proxy_busy_buffers_size 128k;   
  proxy_temp_file_write_size 128k;   

  gzip on;   
  gzip_min_length  1k;   
  gzip_buffers     4 16k;   
  gzip_http_version 1.1;   
  gzip_comp_level 2;   
  gzip_types       text/plain application/x-javascript text/css application/xml;   
  gzip_vary on;   


log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '  
              '$status $body_bytes_sent "$http_referer" '  
              '"$http_user_agent"  $request_time $remote_addr';   
access_log  logs/access.log  main;   
    access_log  on;   
upstream web_app {   
    server 192.168.138.132:8080 weight=1 max_fails=2 fail_timeout=30s;    
    server 192.168.138.133:8080 weight=1 max_fails=2 fail_timeout=30s;   
    server 192.168.138.134:8080 weight=1 max_fails=2 fail_timeout=30s;   
}   
server {   
    listen 80;   
    server_name  192.168.138.132;     
    charset utf-8;  

    location /   
    {   
        proxy_next_upstream http_502 http_504 error timeout invalid_header;   
        proxy_set_header Host  $host;   
        proxy_set_header X-Real-IP $remote_addr;   
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
        proxy_pass http://web_app ;  
        expires      3d;   
    }   

  }   

}

8.关键信息解释:

upstream web_app {   
    server 192.168.138.132:8080 weight=1 max_fails=2 fail_timeout=30s; 
    server 192.168.138.133:8080 weight=1 max_fails=2 fail_timeout=30s;   
    server 192.168.138.134:8080 weight=1 max_fails=2 fail_timeout=30s;   
} 
server {   
    listen 80;   #负载均衡监听端口
    server_name  192.168.138.132;#负载均衡主主机名     
    charset utf-8;  #字符格式

    location /   
    {   
        proxy_next_upstream http_502 http_504 error timeout invalid_header;   
        proxy_set_header Host  $host;   
        proxy_set_header X-Real-IP $remote_addr;   
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
        proxy_pass http://web_app ; #如果有请求到132的80  端口会被随机转发到 132、133、134主机的8080端口 
        expires      3d;   
    }   

  }    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值