UBuntu14.04下搭建nginx+Tomcat服务器集群搭
参考了很多集群服务器搭建,感觉这种方式比较适合我们这种新手,而且又方便又好用的感觉。所以我想搭建一个这样的集群服务器,能让我的集群更加安全可靠。而且nginx的实在是很轻量,只不过确实有致命伤,如果当nginx节点崩了,网站就会全崩。如果大家有什么好的办法解决这种问题可以大家分享下就好了。或者其他可靠的集群。好了,我这是新手的教程,我一般都不太喜欢写原理性的东西。官网啊什么的都更权威,都自己去找吧,我这只是个记录的blog。图个方便备份下过程,具体流程就如下图【来自百度图片】:

一,准备材料
我的是UBuntu14.04,nginx就需要以下的依赖包zlib 库,pcre 库,openssl库,一般你下载最新版就好了,下载tar.gz包
参考:
nginx-1.7.9.tar.gz
zlib-1.2.11.tar.gz
pcre-8.40.tar.gz
openssl-fips-2.0.14.tar.gz
jdk-7u79-linux-x64.tar.gz
2台服务器
1.放nginx
2.放Tomcat




二,开始搭建
首先我们得创建一个nginx文件夹,然后把东西放进去,我们得弄干净点是吧。
cd /
sudo su
mkdir nginx
chmod -R 777 /nginx
然后通过xftp上传文件

然后去解压所有文件
su linux#这是我的Ubuntu用户名
cd nginx/
tar -zxvf nginx-1.7.9.tar.gz
rm -rf nginx-1.7.9.tar.gz
tar -zxvf pcre-8.40.tar.gz
rm -rf pcre-8.40.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
rm -rf zlib-1.2.11.tar.gz
tar -zxvf openssl-fips-2.0.14.tar.gz
rm -rf openssl-fips-2.0.14.tar.gz
mv nginx-1.7.9 nginx
mv openssl-fips-2.0.14 openssl
mv pcre-8.40 pcre
mv zlib-1.2.11 zlib
搭建库环境
sudo apt-get update
sudo apt-get install build-essential #需要C++编译
sudo su
cd pcre
./configure
make
make install
cd ../zlib
./configure
make
make install
cd ../openssl
./config
make
make install
搭建nginx
cd ../nginx/
./configure --with-pcre=../pcre/ --with-zlib=../zlib/ --with-openssl=../openssl/
make
make install



三,测试nginx
cd /usr/local/nginx/sbin
./nginx -t

./nginx
输入一下你的ip:80就能看到了了

成功了哦
四,搭建Tomcat
这时我们先换到另一台服务器进行搭建,等一下我再跟弄nginx的配置,先搭建
首先我们得创建一个java的home,为了同步和干净
sudo mkdir /java
cd /java
sudo su
chmod -R 777 /java
这时候我们同样的通过ftp传送我们下载好的javaJDK,这个我没提供,你们自己去下载,满大街都有

sudo su
tar -zxvf jdk-7u79-linux-x64.tar.gz
rm -rf jdk-7u79-linux-x64.tar.gz
mv jdk1.7.0_79 jdk
vim /etc/profile
#在最后添加上这几句话
export JAVA_HOME=/java/jdk
export JRE_HOME=/java/jdk/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
source /etc/profile
java -version

好了我们开始安装Tomcat,为了干净,我们也创建一个包管理
mkdir /tomcat
cd /tomcat
wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.76/bin/apache-tomcat-7.0.76.tar.gz
tar -zxvf apache-tomcat-7.0.76.tar.gz
rm -rf apache-tomcat-7.0.76.tar.gz
mv apache-tomcat-7.0.76 tomcat
cd tomcat
vim bin/catalina.sh
#输入以下两条语句
#export JAVA_HOME=/java/jdk
#export JRE_HOME=/java/jdk/jre
bin/startup.sh

五,测试Tomcat
在网址上输入ip:8080 即可

六,配置nginx
好了,为了我们先转回nginx那一台服务器去,我们要把Tomcat的服务器要让nginx代理
cd /usr/local/nginx/sbin
./nginx -s quit #关闭nginx
cd /usr/local/nginx/conf/
vim nginx.conf
#user nobody;
#每个进程的最大连接数
worker_processes 2;
#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;
#设置哪些服务器要代理,这里设置自己的
upstream mycluster
{
server 192.168.1.1:8080 weight=1;
}
server
{
#设置代理服务器监听端口
listen 8088;
#设置服务器代理ip,这里设置自己的
server_name 192.168.1.2;
#charset koi8-r;
#access_log logs/host.access.log main;
#一些错误后的处理办法
location /
{
proxy_pass http://mycluster;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
root html;
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;
# }
#}
}
重启服务
cd ../sbin
./nginx
七,测试nginx
在浏览器输入nginx服务器的ip 192.168.1.2:8088能看到这个就证明成功了啊。

八,增加节点
现在我才2台服务器,一台是nginx,一台是tomcat,为了验证这种代理效果,我再外加一台tomcat,我们来看看效果怎么样。同样按照刚刚配置tomcat的步骤,在做一个服务器,并启动。
然后我们在nginx服务器上,配置一下nginx.conf,就在这个位置多加一条服务器的语句

然后你会问,如果这样的话,我加一个节点不就得重启那个nginx服务器,不就会很容易崩?你太低估nginx,它的轻量让你无法想象,就输入一条语句。就代表了重启了。
cd /usr/local/nginx/sbin #这条语句不算,这只是为了怕你走错位置了
./nginx -s reload

你说一模一样啊。。怎么看出不同的节点和调度呢?等下最后一步就在这里了
九,测试集群效果
在所有的tomcat服务器都做这个操作,并且分别输入不同的标记
cd /tomcat/tomcat/webapps/
mkdir drp
cd drp
vim login.html
#<html>
# <head>
# <meta http-equiv="content-type" content="text/html;charset=gb2312" />
# </head>
# <body>
# 小朋友们2
# </body>
#</html>
在浏览器中输入ip:8088/drp/login.html (多刷几次)


你就会发现,同一个ip,居然会有两种不同的访问,这就好了憋。成功!,自己多查查nginx,配置,看下有什么可以配置的使反向代理服务器更好的服务于你