设为首页 收藏本站
查看: 488|回复: 0

[经验分享] nginx+apache+redis实现负载均衡、动静分离、session共享

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-5 09:31:32 | 显示全部楼层 |阅读模式
环境centos6.5
nginx:192.168.1.202
tomcat1:192.168.1.240
tomcat2:192.168.1.201
redis:192.168.1.116


nginx安装:
1
yum install -y nginx



如提示包不存在,安装epel源即可
1
rpm -ivh https://dl.fedoraproject.org/pub ... latest-6.noarch.rpm



修改nginx配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
vim /etc/nginx/conf.d/default.conf
user nginx;
worker_processes 2;
  
error_log/usr/local/nginx/logs/nginx_error.log debug;
pid /usr/local/nginx/nginx.pid;
  
worker_rlimit_nofile 51200;
  
events {
         use epoll;
         worker_connections 51200;
}
  
http {
         includemime.types;
         default_type application/octet-stream;
         #charset gb2312;
         server_names_hash_bucket_size 128;
         client_header_buffer_size 32k;
         large_client_header_buffers 4 32k;
         client_max_body_size 8m;
         sendfile on;
         tcp_nopush on;
         tcp_nodelay on;
         keepalive_timeout 60;
         server_tokens   off;
         client_body_buffer_size  512k;
  
         gzip on;
         gzip_min_length 1k;
         gzip_buffers 16 64k;
         gzip_http_version 1.0;
         gzip_comp_leve l2;
         gzip_types text/plain application/x-javascript text/css application/xml;
         gzip_vary on;
         #limit_zone crawler $binary_remote_addr 10m;
  
         log_formataccess '$remote_addr - $remote_user [$time_local] "$request" '
         '$status$body_bytes_sent "$http_referer" '
         '"$http_user_agent"$http_x_forwarded_for';
         access_log/usr/local/nginx/logs/access.log access;
  
         upstream test {
                   server192.168.1.201:8081 weight=1 max_fails=2 fail_timeout=30s;
                   server192.168.1.240:8081 weight=1 max_fails=2 fail_timeout=30s;
         }
  
server {
         listen80;
         server_name192.168.1.202;
  
location / {
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         index  index_tel.jsp index.jsp index.html index.htm;
       proxy_pass http://test;
         proxy_redirect    off;
       proxy_set_header Host  $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_connect_timeout    5;
         proxy_send_timeout      60;
         proxy_read_timeout       5;
         proxy_buffer_size       16k;
         proxy_buffers         4 64k;
         proxy_busy_buffers_size  128k;
         proxy_temp_file_write_size 128k;
    }
  
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|otf|eot|svg|ttf|woff)$
{
valid_referers  blocked 192.168.1.202;
if ($invalid_referer) {
         return421;
}
  
expires 30d;
}
  
location ~ .*\.(js|css)?$
{
root /usr/local/nginx/html;
expires 1h;
}
  
}
}




安装jdk和tomcat(1.240、1.201配置相同)
jdk这里使用1.7.0_40版本绿色免安装的
1
2
3
4
5
6
7
tar -zxvf jdk-7u40-linux-x64.tar.gz  -C  /usr/local/
cat > /etc/profile.d/java.sh <<EOF
export JAVA_HOME=/usr/local/jdk1.7.0_40
export PATH=$PATH:$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
EOF




tomcat使用7.0.52版本的
1
tar -zxvf apache-tomcat-7.0.52.tar.gz -C/usr/local/tomcat




tomcat-redis-session-manager组件实现session共享
1
2
3
wget https://github.com/downloads/jco ... tomcat-7-Java-7.jar
wget http://central.maven.org/maven2/ ... 5.2/jedis-2.5.2.jar
wget http://central.maven.org/maven2/ ... mmons-pool2-2.0.jar





将下载的jar包放置tomcat的lib目录下

修改tomcat下的context.conf文件
1
2
3
4
5
6
<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
   <ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
       host="192.168.1.116"
       port="6379"
       database="0"
       maxInactiveInterval="60" />




1
wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz



1
tar -zxvf redis-2.6.13.tar.gz



1
cd redis-2.6.13 && make && make install



1
make test




1
mkdir -pv /usr/local/redis/{bin,etc}



将src目录里面的redis-benchmark redis-check-aof redis-check-dump  redis-cli  redis-server拷贝至/usr/local/redis/bin下面
将redis.conf拷贝至/usr/local/redis/etc目录下
修改redis.conf
1
2
3
daemonize yes #开启后台运行
# bind 127.0.0.1 ::1注释掉
protected-mode no #关闭保护模式






测试
1
mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib}



1
vim index.jsp



192.168.1.240:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
   <h1><font color="red">1.240</font></h1>
   <table border="1">
     <tr>
       <td>Session ID</td>
   <% session.setAttribute("1.240","1.240"); %>
       <td><%= session.getId() %></td>
     </tr>
     <tr>
       <td>Created on</td>
       <td><%= session.getCreationTime() %></td>
    </tr>
   </table>
</body>
</html>




192.168.1.201
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
   <h1><font color="blue">1.201</font></h1>
   <table border="1">
     <tr>
       <td>Session ID</td>
    <%session.setAttribute("1.201","1.201"); %>
       <td><%= session.getId() %></td>
     </tr>
     <tr>
       <td>Created on</td>
       <td><%= session.getCreationTime() %></td>
    </tr>
   </table>
</body>
</html>



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-239602-1-1.html 上篇帖子: Keepalived+Nginx架构 下篇帖子: nginx动静分离
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表