nginx+apache+redis實現負載平衡、動靜分離、session共用

來源:互聯網
上載者:User

標籤:設定檔   local   events   

環境centos6.5

nginx:192.168.1.202

tomcat1:192.168.1.240

tomcat2:192.168.1.201

redis:192.168.1.116



nginx安裝:

yum install -y nginx

如提示包不存在,安裝epel源即可

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

修改nginx設定檔

vim /etc/nginx/conf.d/default.confuser 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版本綠色免安裝的

tar -zxvf jdk-7u40-linux-x64.tar.gz  -C  /usr/local/cat > /etc/profile.d/java.sh <<EOFexport JAVA_HOME=/usr/local/jdk1.7.0_40export PATH=$PATH:$JAVA_HOME/binexport JRE_HOME=$JAVA_HOME/jreexport CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/libEOF

 

tomcat使用7.0.52版本的

tar -zxvf apache-tomcat-7.0.52.tar.gz -C/usr/local/tomcat

 

tomcat-redis-session-manager組件實現session共用

wget https://github.com/downloads/jcoleman/tomcat-redis-session-manager/tomcat-redis-session-manager-1.2-tomcat-7-Java-7.jarwget http://central.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jarwget http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.0/commons-pool2-2.0.jar

 

 

將下載的jar包放置tomcat的lib目錄下

 

修改tomcat下的context.conf檔案

<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>   <ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"       host="192.168.1.116"       port="6379"       database="0"       maxInactiveInterval="60" />

 

wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
tar -zxvf redis-2.6.13.tar.gz
cd redis-2.6.13 && make && make install
make test

 

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

daemonize yes #開啟後台運行# bind 127.0.0.1 ::1注釋掉protected-mode no #關閉保護模式


 

測試

mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib}
vim index.jsp

192.168.1.240:

<%@ 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

<%@ 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>

 


本文出自 “linux_cheng” 部落格,請務必保留此出處http://6764097.blog.51cto.com/6754097/1795671

nginx+apache+redis實現負載平衡、動靜分離、session共用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.