Nginx、Redis、Tomcat负载均衡及session共享实现
一、nginx安装配置
1.安装编译工具及库文件
yum -y install make
zlib zlib-devel gcc-c++ libtool openssl
openssl-devel
2.安装 PCRE,PCRE 作用是让 Ngnix 支持 Rewrite 功能
#wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
#tar zxvf
pcre-8.35.tar.gz
#cd pcre-8.35
#./configure
#make && make
install
3.安装 Nginx
#wget http://nginx.org/download/nginx-1.11.6.tar.gz
#tar zxvf
nginx-1.11.6.tar.gz
#cd nginx-1.11.6
#./configure
(--prefix=/usr/local/webserver/nginx --with-http_stub_status_module
--with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35可忽略)
#make && make
install
#/usr/local/webserver/nginx/sbin/nginx
-v查看安装的nginx版本
注:
Nginx默认安装在/usr/local目录下:
/usr/local/nginx/sbin/nginx
-t 检查配置文件ngnix.conf的正确性命令
/usr/local/nginx/sbin/nginx 启动 Nginx
/usr/local/nginx/sbin/nginx
-s stop 停止Nginx
/usr/local/nginx/sbin/nginx
-s reopen 重启 Nginx
/usr/local/nginx/sbin/nginx
-s reload 重新载入配置文件
参考资料:
http://www.runoob.com/linux/nginx-install-setup.html
1.设置环境变量(/etc/profile):
CATALINA_BASE=/home/lgip_fastdfs/apache-tomcat-7.0.73
CATALINA_HOME=/home/lgip_fastdfs/apache-tomcat-7.0.73
export CATALINA_BASE
CATALINA_HOME
CATALINA_2_BASE=/home/lgip_fastdfs/apache-tomcat-7.0.73-2
CATALINA_2_HOME=/home/lgip_fastdfs/apache-tomcat-7.0.73-2
export CATALINA_2_BASE
CATALINA_2_HOME
TOMCAT_HOME=/home/lgip_fastdfs/apache-tomcat-7.0.73
export TOMCAT_HOME
TOMCAT_2_HOME=/home/lgip_fastdfs/apache-tomcat-7.0.73-2
export TOMCAT_2_HOME
#source /etc/profile
2.第二个shutdown.sh、startup.sh增加:
export
CATALINA_HOME=$CATALINA_2_HOME
export
CATALINA_BASE=$CATALINA_2_BASE
以此类推...
多个tomcat分别启动、停止即可,不会造成冲突。
若tomcat停止无效,则使用
#ps -ef|grep tomcat
#kill -9 进程id 强制杀死进程
三、Nginx配置详解
这是实测可正常使用的配置文件:
#user nobody;
#Nginx用户及组
worker_processes 1; #工作进程的数量,通常等于CPU数量或者2倍于CPU
(worker_processes
auto;
worker_cpu_affinity
auto;这样设置进程自动寻找cpu)
#错误日志:存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid(进程标识符):存放路径
#pid logs/nginx.pid;
#最大连接数 = worker_processes *
worker_connections/4
events {
worker_connections 1024;#工作进程的最大连接数量
}
http {
#设定mime类型,类型由mime.type文件定义
include mime.types;
default_type application/octet-stream;
#(日志格式设置。
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent
:记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。)
log_format
main '$remote_addr - $remote_user
[$time_local] "$request" '
'$status $body_bytes_sent
"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#用log_format指令设置日志格式后,需要用access_log指令指定日志文件的存放路径;
#access_log
logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip
on;
upstream 192.168.20.35 {
server
192.168.20.35:9001 weight=3 max_fails=2 fail_timeout=30s;
server
192.168.20.35:9002 weight=3 max_fails=2 fail_timeout=30s;
ip_hash;
}
server {
listen
8111;
server_name
192.168.20.35;
index index.jsp index.html index.htm;
#配置发布目录
root
/home/lgip_fastdfs/apache-tomcat-7.0.73/webapps/ROOT;
location /
{
proxy_next_upstream http_502 http_504
error timeout invalid_header;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP
$remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_pass http://192.168.20.35;
#expires 3d;
}
#动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstream 均衡
location ~ .*\.(php|jsp|cgi)?$
{
#root
/home/lgip_fastdfs/apache-tomcat-7.0.73/webapps/ROOT/WEB-INF;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP
$remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_pass http://192.168.20.35;
}
#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
location ~
.*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root
/home/lgip_fastdfs/apache-tomcat-7.0.73/webapps/ROOT/WEB-INF;
#expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
expires
3d;
}
#定义Nginx输出日志的路径
access_log
/home/lgip_fastdfs/data/logs/nginx_lgip/access.log main;
error_log
/home/lgip_fastdfs/data/logs/nginx_lgip/error.log crit;
}
# 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;
#
}
#}
}
参考资料:
http://blog.csdn.net/tjcyjd/article/details/50695922
四、Redis、Tomcat配置:
1.Redis安装运行:
#wget http://download.redis.io/releases/redis-2.8.13.tar.gz
#tar zxvf
redis-2.8.13.tar.gz
#cd redis-2.8.13
#make
执行make命令,最后几行的输出结果
Hint: To
run ‘make test’ is a good idea ;)
make[1]:
Leaving directory `/opt/redis-2.8.13/src’
#cd src&&make
install
默认情况,Redis不是在后台运行,我们需要把redis放在后台运行
#vim
/usr/local/redis/etc/redis.conf
将daemonize的值改为yes
#或者加上`&`号使redis以后台程序方式运行
#./redis-server &
客户端连接
#./redis-cli
2.Tomcat配置:
修改tomcat文件夹中conf/context.xml文件,在context节点下添加如下配置:
<Valve
className="com.radiadesign.catalina.session.RedisSessionHandlerValve"
/>
<Manager
className="com.radiadesign.catalina.session.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60" />
参考资料:
http://www.runoob.com/redis/redis-tutorial.html
https://my.oschina.net/liting/blog/535273
http://blog.csdn.net/chszs/article/details/42610365
评论
发表评论