nginx1.4+tocat7 负载均衡(在一台机器上按两个tomcat)

实践环境:
系统版本:CentOS6.4 X86_64
Nginx版本:Nginx-1.4.5
Tomcat版本:Tomcat-7.0
虚拟机10.10.54.61
############################
源码编译nginx1.4.5
1.下载
[root@gyf  soft]#wget http://nginx.org/download/nginx-1.4.5.tar.gz
[root@gyf  soft]# tar xvf nginx-1.4.5.tar.gz
2.编译
yum install openssl-devel.X86_64 make pcre
[root@gyf  nginx-1.4.5]# ./configure --prefix=/usr/local/nginx --user=apache --group=apache --with-http_stub_status_module   --with-http_gzip_static_module --with-http_ssl_module
///
--with-http_stub_status_module     enable ngx_http_stub_status_module     ---支持监控
--with-http_gzip_static_module     enable ngx_http_gzip_static_module     ---支持压缩

3.安装
[root@gyf  nginx-1.4.5]# make && make install
4.启动
[root@gyf  conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] getpwnam("apache") failed
[root@gyf  conf]# useradd apache

[root@gyf  conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@gyf  conf]# netstat -ntlp|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1572/httpd          
[root@gyf  conf]# apachectl stop
[root@gyf  conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5.关闭:给父进程发送一个TERM信号,试图杀死它和它的子进程。
[root@s01 logs]# cat /usr/local/nginx/logs/nginx.pid | xargs kill -TERM

6.重启
[root@s01 logs]# cat /usr/local/nginx/logs/nginx.pid | xargs kill -HUP

HUP      重启
TERM,INT 快速停止
USR1    重新打开日志文件,用于日志切割
USR2    平滑升级可执行程序
QUIT     从容关闭
WINCH    从容关闭工作进程

//测试配置文件
[root@s01 html]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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

//nginx配置文件格式:

user  apache apache;
worker_processes  2;   ---cpu核-1
....

event{}
http{
....
upstream {}
server{ location{} }

}

#############################
安装tomcat

1.下载jdk-7u51-linux-x64.rpm
rpm -ivh jdk-7u51-linux-x64.rpm
2.添加环境变量,配置jdk
[root@gyf  soft]# find / -name '*jdk*'
/usr/java/jdk1.7.0_51
[root@gyf  profile.d]# vim /etc/profile
JAVA_HOME=/usr/java/jdk1.7.0_51
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib     
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin     
export PATH CLASSPATH JAVA_HOME
[root@gyf  soft]# source /etc/profile
[root@gyf  soft]#java -version
3.安装tomcat
[root@gyf  soft]# tar xvf apache-tomcat-7.0.52.tar.gz
[root@gyf  soft]# mv apache-tomcat-7.0.52 /usr/local/tomcat

4.做tomcat自动启动脚本
[root@gyf  soft]# cd /usr/local/tomcat/bin/
[root@gyf  bin]# cp catalina.sh /etc/init.d/tomcat
[root@gyf  bin]# chmod +x /etc/init.d/tomcat
[root@gyf  init.d]# vim tomcat
#chkconfig: 2345 88  99
#description: tomcat
//jdk目录
:96  JAVA_HOME=/usr/java/jdk1.7.0_51
//tomcat目录
CATALINA_HOME=/usr/local/tomcat
[root@gyf  bin]# chkconfig --add tomcat
5.启动tomcat
[root@gyf  soft]# /etc/init.d/tomcat start
在浏览器中搜索10.10.54.61:8080出现tomcat原始页面即安装成功
#################
一台机器同时运行两个tomcat
上面前两步无需在做
1.安装tomcat到新目录
[root@gyf  soft]# tar xvf apache-tomcat-7.0.52.tar.gz
[root@gyf  soft]# mv apache-tomcat-7.0.52 /usr/local/tomcat2

2.编辑tomcat/conf/server.xml,将所有端口号8:开头都改为9开头(其他端口只要不冲突即可,目的是不让多个的Tomcat的端口号冲突)
修改3个地方
第一:
<Server port="9005" shutdown="SHUTDOWN"> ----原来是8005,关闭端口号
第二
<Connector port="9080" maxHttpHeaderSize="8192" ----原来是8080,http链接端口号:1:
第三<Connector port="9009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" /> ---原来是8009,jvm即java虚拟机访问端口
3.启动
[root@gyf  tomcat2]# ./bin/startup.sh
4.查看进程两个
[root@gyf  tomcat2]# ps -ef |grep tomcat
7.做tomcat自动启动脚本
[root@gyf  soft]# cd /usr/local/tomcat2/bin/
[root@gyf  bin]# cp catalina.sh /etc/init.d/tomcat2
[root@gyf  bin]# chmod +x /etc/init.d/tomcat2
[root@gyf  init.d]# vim tomcat2
#chkconfig: 2345 85 96
#description: tomcat
:96 JAVA_HOME=/usr/java/jdk1.7.0_51
    CATALINA_HOME=/usr/local/tomcat2

[root@gyf  bin]# chkconfig --add tomcat2
8.重启tomcat2
[root@gyf  soft]# /etc/init.d/tomcat2 start
####tomcat配置用户管理
[root@gyf  conf]# vim /usr/local/tomcat/conf/tomcat-users.xml
  <role rolename="manager-gui"/>
  <role rolename="admin-gui"/>
  <user username="tomcat" password="tomcat" roles="admin-gui,manager-gui"/>

##################
配置nginx
1.配置代理文件
[root@gyf  htdocs]# cd /usr/local/nginx/conf/
[root@gyf  conf]#mkdir virtual
[root@gyf  conf]# vim virtual/bbs.ssr.com.conf
upstream bbs_ssr_com {
    server localhost:9080 max_fails=3 weight=1 fail_timeout=60s;
    server localhost:8080 max_fails=3 weight=3 fail_timeout=60s;
    }


server {
        listen      80;
        server_name bbs.ssr.com;
        location / {
    #jsp网站程序根目录,一般nginx与tomcat在同一个目录
    #        root  /usr/local/tomcat/webapps/ROOT;
         root /var/www/bbs
            index  index.html index.jsp index.html;
        }
location ~ .*\.(gif|jpg|png|bmp|swf)$  #由nginx处理静态页面
        {
        expires 30d;  #使用expires缓存模块,缓存到客户端30天
        }

   location ~ .*\.(js|css)?$ {
        expires      1h;
        }

location ~ (\.jsp)|(\.do)$ {
        index index.jsp ;
        proxy_pass http://bbs_ssr_com;  #来自jsp请求交给tomcat处理
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP$remote_addr;
        proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
        proxy_redirect off;
 client_max_body_size 10m;      #允许客户端请求的最大单文件字节数
 client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数,
 proxy_connect_timeout 90;      #nginx跟后端服务器连接超时时间(代理连接超时)
 proxy_read_timeout 90;          #连接成功后,后端服务器响应时间(代理接收超时)
 proxy_buffer_size 4k;          #设置代理服务器(nginx)保存用户头信息的缓冲区大小
 proxy_buffers 6 32k;            #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
 proxy_busy_buffers_size 64k;    #高负荷下缓冲大小(proxy_buffers*2)
 proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }
        
}


2..主配置文件配置

[root@gyf  ~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  2;
error_log  logs/error.log info;
pid      logs/nginx.pid;
events {
    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;
keepalive_timeout  65;
#gzip压缩功能设置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascripttext/css application/xml;
    gzip_vary on;

include virtual/bbs.ssr.com.conf;

}

3.配置tomcat
[root@gyf  conf]# vim /usr/local/tomcat/conf/server.xml
#确认8080端口
 :70     <Connector port="8080" protocol="HTTP/1.1"
 71                connectionTimeout="20000"
 72                redirectPort="8443" />

123       <Host name="localhost"  appBase="webapps"
124             unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/bbs" />
125 如果网站没有放在tomcat默认根目录/usr/local/tomcat/webapps/ROOT/
时添加<Context path="" docBase="/var/www/bbs" />

[root@gyf  conf]# vim /usr/local/tomcat2/conf/server.xml
#确认9080端口
 :70     <Connector port="8080" protocol="HTTP/1.1"
 71                connectionTimeout="20000"
 72                redirectPort="8443" />

123       <Host name="localhost"  appBase="webapps"
124             unpackWARs="true" autoDeploy="true">       
            <Context path="" docBase="/var/www/bbs" />
4.建网页
[root@gyf  www]#mkdir -p /var/www/bbs
[root@gyf  bbs]# vim index.html
html
[root@gyf  bbs]# vim index.jsp
jsp
[root@gyf  bbs]# vim index.do
do
5.重启服务
[root@gyf  ~]# /etc/init.d/nginx restart
[root@gyf  ~]# /etc/init.d/tomcat restart
[root@gyf  ~]# /etc/init.d/tomcat2 restart

6.测试
在ubuntu10.10.54.60
gyf@gyf :~$ vim /etc/hosts
10.10.54.60  bbs.ssr.com

在搜索应请输入bbs.ssr.com
显示html
输入bbs.ssr.com/index.jsp
http://10.10.54.61/index.jsp
显示jsp

输入bbs.ssr.com/index.do
http://10.10.54.61/index.do
显示do




你可能感兴趣的:(负载均衡,nginx1.4+tocat7,nginx编译,tomcat编译)