nginx 搭建及tomcat集群、session共享(redi
2017-01-16 本文已影响0人
在路上_Rogge
- 环境
centos6.5 nginx-1.8.1 tomcat7.0.75 redis2.8 - nginx搭建
- 安装nginx 依赖包
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
- 安装nginx
\#tar -zxvf nginx-1.8.1.tar.gz //解包
\#mv nginx-1.8.1 nginx //进入目录
\#cd nginx //进入目录
./configure --user=chenshb --group=chenshb --prefix=/data1/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-cc-opt='-O3' --with-cpu-opt=opteron --with-http_gzip_static_module
//面我们指定把nginx安装到/data1/nginx目录下并且安装Nginx性能检测模块和SSL安全模块以及FLV模块支持
make && make install //编译和安装
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local //添加到开机自启动
- 目录说明:4个文件目录 conf-->配置文件 html--->程序目录 logs--->日志目录 sbin--->命令目录
- Nginx相关操作命令
# /usr/local/nginx/sbin/nginx // 启动nginx
# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` //停止nginx
#kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //重启nginx
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}' 输入此命令查看Nginx主进程号屏幕显示的即为Nginx主进程号,例如: 6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
#kill -HUP 6302- 平滑重启nginx:(比如你编辑了nginx.conf配置了文件,那么无需要重新启动nginx
执行以下命令即可立即加载!)
对于Nginx 0.8.x以上的版本,现在平滑重启Nginx配置非常简单,执行以下命令即可:
#/usr/local/nginx/sbin/nginx -s reload
#ps -aux | grep nginx
- 配置nginx (具体可参考nginx配置文件的详解)
- 修改nginx.conf
# erdo
user root;
worker_processes 4;
events {
worker_connections 8192;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 200m;
client_body_temp_path /usr/local/nginx/client_body_temp;
fastcgi_buffers 4 64k;
fastcgi_buffer_size 64k;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_disable "msie6";
gzip_min_length 1024;
gzip_http_version 1.0;
gzip_types text/plain text/xml application/xml text/javascript text/css application/x-javascript application/javascript;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For2 $proxy_add_x_forwarded_for;
#add_header X-Frame-Options "SAMEORIGIN";
# deny ip/empty_server_name access
server {
listen 80;
server_name _;
return 403;
}
include /usr/local/nginx/conf/vhost.d/*conf;
}
# vim:set et ts=2 sw=2: #
- 在目录vhost.d下增加tomcatfedin.conf
upstream turn_server {
server localhost:80;
}
upstream tomcat {
#ip_hash;
server 192.168.172.102:8080 max_fails=3 weight=1 fail_timeout=60s;
server 192.168.172.102:8081 max_fails=3 weight=1 fail_timeout=60s;
}
server {
listen 80;
server_name 192.168.172.102;
server_name_in_redirect off;
proxy_set_header Host $host;
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;
access_log /usr/local/nginx/logs/error_log;
error_log /usr/local/nginx/logs/error_log;
root /usr/local/nginx/html;
index index.php index.html;
# issues1096
if ($request_uri ~ " ") {
return 404;
}
# issues529 940
location ~* ^/(gtyc|web)/login\.php {
return 404;
}
location ~* ^/tool/dologin\.php {
return 404;
}
# issues529 940
#location ~* ^/ebook/(book_index|denglu|dinggou)\.php {
# return 404;
# }
location ~* ^/ePubManager {
proxy_pass http://turn_server;
}
#tomcat
location ~* ^/tf/tm1 {
proxy_pass http://localhost:8080;
}
location ~* ^/tf/tm2 {
proxy_pass http://localhost:8081;
}
location ~* ^/docs {
proxy_pass http://tomcat;
}
location ~* {
# proxy_pass http://turn_server;
proxy_pass http://tomcat;
}
# include /usr/local/nginx/conf/vhost.d/comm;
}
- tomcat 安装(具体可以参考网络上)
安装两个相同配置的tomcat8080 tomcat8081
在tomcat ROOT下新建JSP文件
unzip apache-tomcat-6.0.44.zip
mv apache-tomcat-6.0.44 tomcat6
-
启动nginx tomcat与简单测试
修改ROOT 目录下index.html 在各自的服务器上加入8080或8081;访问http://192.168.172.102
则轮替出现如下图所示;表示已完成nginx+tomat集群的搭建
8080.png
8081.png -
tomcat集群间session的共享
-
session 存在数据库中(会加大数据库的IO,增加数据库的负担)
-
session存在memcache或者redis中
-
ngin中x的ip_hash,能够将某个IP的请求定向到同一台后端;
upstream nginx.example.com
{
server 192.168.172.102:8081;
server 192.168.172.102:8080;
ip_hash;
}
server
{
listen 80;
location /
{
proxy_pass
http://nginx.example.com;
}
}
- session共享
- redis 配置 192.168.172.102:6379
- 两个tomcat7配置: 在server.xml或context.xml中配置
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.172.103"
port="6379"
database="0"
maxInactiveInterval="60"/>
建议配置在context.xml 需在$tomcat/lib/下加增加JAR包:commons-pool-1.6.jar、jedis-2.1.0.jar、tomcat-redis-session-manager-1.2-tomcat-7.jar 下载地址。
- tomcat 第二种配置 如果需要支持redis集群的配置时,可以参考来开源项目https://github.com/jcoleman/tomcat-redis-session-managers; tomcat配置:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.172.103"
port="6379"
database="0"
maxInactiveInterval="60"/>
此配置及jar包下载参考https://github.com/izerui/tomcat-redis-session-manager;
- 测试session 共享示例
在两个tomcat7 下加入 test.jsp
<%@ page language="java" %>
<html>
<head><title>Tomcat8081</title></head>
<body>
<h1><font color="red">Tomcat8081.test</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("test","test"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
测试效果:
tomcat_8080
tomcat_8081
出现以上效果说是有session共享成功