Nginx 安装

yum -y install pcre*

yum -y install pcre*


tar -zxvf nginx-1.7.8.tar.gz

然后进入目录编译安装

cd nginx-1.7.8

./configure --prefix=/usr/local/nginx(必须先建好目录)

如果没有error信息,就可以执行下边的安装了:

make

make install

4、开启nginx进程


/usr/local/nginx-1.7.8/sbin/nginx

重启或关闭进程:

/usr/local/nginx-1.7.8/sbin/nginx -s reload

/usr/local/nginx-1.7.8/sbin/nginx -s stop

5、关闭防火墙,或者添加防火墙规则就可以测试了。

service iptables stop


6.配置nginx,实现tomcat负载均衡

vim /etc/nginx/nginx.conf
#修改events的块内容
events {
use epoll;
worker_connections 2048;
}
#在http块里面增加如下内容
#开启zip网页压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml;

#反向代理
## meerkat_web
upstream meerkat_web {
#ip_hash; #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
server 192.168.1.215:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.216:8080 weight=2 max_fails=2 fail_timeout=30s;
}
# 在server块里面的location部分替换成如下内容
location / {
root html;
index index.html index.htm index.jsp;
proxy_pass http://meerkat_web;
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 100m;
}

 


7.测试

a.测试自动切换
访问http://192.168.1.217/ 代表的nginx服务器,会出现index.jsp显示的效果。这个时候如果关闭对应的tomcat服务,就会自动切换到另外一台上。
b.测试负载均衡
访问http://192.168.1.217/ 代表的nginx服务器,会出现index.jsp显示的效果。但是多次刷新后,一直显示的是同一台tomcat服务。说明负载均衡没有实现。解决的办法是把upstream里面的 ip_hash 这个给屏蔽掉。就会自动切换了。

yum -y install memcached
/etc/rc.d/init.d/memcached start

接下来,可以用以下命令启动与停止 memcached

/etc/rc.d/init.d/memcached start
/etc/rc.d/init.d/memcached stop
/etc/rc.d/init.d/memcached restart

posted @ 2018-03-20 17:17  新手----起步  阅读(186)  评论(0编辑  收藏  举报