软件准备:
(1)jdk-8u73-linux-x64.tar.gz
(2)apache-tomcat-7.0.57.tar.gz
(3)nginx-1.7.7.tar.gz
准备3台Linux机器,我的3台Linux主机IP分别是192.168.71.11、192.168.71.12和192.168.71.13。
我们打算在3台Linux机器上各安装1台tomcat,并在第1台机器上安装一个nginx。
一、安装JDK
解压jdk安装包
tar -zxvf jdk-8u73-linux-x64.tar.gz
解压jdk安装包,得到一个解压后的目录:/usr/local/zhaopin/jdk1.8.0_73,后面tomcat要使用到这个目录。
二、安装和配置tomcat
(2)解压tomcat安装包
tar -zxvf apache-tomcat-7.0.57.tar.gz
解压tomcat安装包,得到一个解压后的目录,将其改名为"tomcat-kafka-8301",即:/usr/local/zhaopin/tomcat-kafka-8301,后面配置tomcat也要到此目录。
(3)修改tomcat-kafka-8301/bin/catalina.sh文件,在开头添加如下内容:
CATALINA_HOME=/usr/local/zhaopin/tomcat-kafka-8301
JAVA_HOME=/usr/local/zhaopin/jdk1.8.0_73
JAVA_OPTS="-Xms2048m
-Xmx2048m
-Xmn384m
-Xss256k
-XX:PermSize=128M
-XX:NewSize=384m
-XX:MaxNewSize=384m
-XX:MaxPermSize=256m
-XX:ParallelGCThreads=8
-XX:+UseConcMarkSweepGC
-XX:+UseCMSCompactAtFullCollection
-XX:+CMSParallelRemarkEnabled
-XX:CMSInitiatingOccupancyFraction=80
-Dcom.sun.management.jmxremote.port=7777
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=192.168.71.11
-XX:+HeapDumpOnOutOfMemoryError
-Djava.awt.headless=true"
(4)修改server.xml文件的内容:
<?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina">
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="1000" minSpareThreads="50" maxIdleTime="60000"/> <Connector executor="tomcatThreadPool" port="8301" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" enableLookups="false" maxPostSize="10485760" URIEncoding="UTF-8" acceptCount="100" acceptorThreadCount="2" disableUploadTimeout="true" maxConnections="10000" SSLEnabled="false"/> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server>
(5)修改tomcat-users.xml文件,在末尾添加如下内容:
<role rolename="manager"/> <role rolename="manager-gui"/> <role rolename="admin"/> <role rolename="admin-gui"/> <user username="admin" password="admin" roles="admin-gui,admin,manager-gui,manager"/>
这样一台tomcat就配置好了,按照此方法在多台服务器上安装多个tomcat即可。
为了便于观察nginx的负载均衡效果,我们需要分别修改3台tomcat服务器的默认主页(修改tomcat安装目录下的/webapps/ROOT/index.jsp文件),在页面顶部添加本地的IP地址,比如:
三、安装和配置nginx
(1)使用yum命令安装nginx的编译环境
yum -y install gcc-c++
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl—devel
(2)使用wget命令下载nginx安装包
wget http://nginx.org/download/nginx-1.7.7.tar.gz
(3)解压nginx安装包
tar -zxvf nginx-1.7.7.tar.gz
(4)进入到解压后的nginx目录
(5)编译nginx到指定的目录
./configure --prefix=/usr/local/zhaopin/nginx
(5)安装nginx
make && make install
(6)启动、停止、重启nginx的命令
启动: sbin/nginx
停止: sbin/nginx -s stop
重启: sbin/nginx -s reload
(7)在浏览器中访问 http://ip地址,如果能够看到nginx首页说明nginx安装成功
如果访问不到nginx的首页,可能是防火墙问题,安装如下配置防火墙,并在浏览器中重新访问即可:
防火墙打开80端口
service iptables stop //关闭防火墙
如果在浏览器中访问不了nginx,请在防火墙中将80端口添加例外:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
将防火墙配置做持久化:
/etc/rc.d/init.d/iptables save
查看防火墙的状态:
/etc/init.d/iptables status
(8)nginx.conf配置
#user nobody;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/xml text/javascript text/css text/plain application/json application/xml application/javascript;
gzip_vary on;
upstream backend_server {
server 192.168.71.11:8301;
server 192.168.71.12:8302;
server 192.168.71.13:8303;
#ip_hash;
}
server {
listen 8081;
server_name backend_server;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html; #定义服务器的默认网站根目录位置
#index index.html index.htm; #定义首页索引文件的名称
proxy_pass http://backend_server; #请求转向backend_server 定义的服务器列表
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 120s; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 128k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 256k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
#proxy_busy_buffers_size 256k; #高负荷下缓冲大小(proxy_buffers*2)
#proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
#proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header x-real-ip $remote_addr;
}
#location / {
#root /home/ftpuser/www;
#index index.html index.htm;
#}
#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;
#}
}
# another virtual 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;
# }
#}
# HTTPS server
#
#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;
# }
#}
}
在上面的配置文件中,我们安装了3台tomcat,端口分别是8301、8302和8303,修改好nginx的配置文件之后,重启nginx,并在浏览器中访问:http://192.168.71.11,并不断刷新页面,如果看到tomcat默认主页顶部有ip地址在变化说明nginx和tomcat负载均衡配置成功。