nginx+tomcat(整合+負載均衡)+memcachedjavascript
一.ngiux+tomcat整合css
1.安裝jdk (搭建jave包的環境)
下載 jdk-6u26-linux-x64.bin
sh jdk-6u26-linux-x64.bin
mv jdk1.6.0_26/ /usr/local/jdk
vim /etc/profile
nset pathmunge
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
2安裝tomcat
下載tomcat
tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local/tomcat
/usr/local/tomcat/bin/start.sh(啓動tmcat)
netstat -ntal |grep 8080 (查看端口)
tcp 0 0 :::8080 :::* LISTEN
在瀏覽器裏面輸入本身的ip+:8080 就能看到tmcat家的小貓
3 安裝nginx
yum install pcre-devel openssl-devel gcc zlib-devel -y
下載nginux
tar zxf nginx-1.0.2.tar.gz
cd nginx-1.0.2
vi auto/cc/gcc (174-175行)
# debug
#CFLAGS=」$CFLAGS -g」 (註釋掉這行,去掉debug模式編譯,編譯之後程序只有幾百k)
vi src/core/nginx.h
#define NGINX_VERSION "1.0.6" (11-13行)
#define NGINX_VER "nginx" (修改此行,去掉後面的「NGINX_VERSION」,爲了安全,這樣編譯
後外界沒法獲取程序的版本號)
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx; #使用的用戶和組
worker_processes 8; #指定工做衍生進程數
error_log logs/error.log info; #錯誤日誌定義類型
pid logs/nginx.pid; #指定 pid 存放的路徑
events {
use epoll; #使用高效網絡I/O模型 具體內容查看 http:/wiki.codemongers.com/事件模型
worker_connections 1024; #容許的鏈接數
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #日誌設定
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
server_names_hash_bucket_size 128; #服務器名字的hash表大小
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 65; 超時時間
gzip on;
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩衝區
gzip_http_version 1.0; #壓縮版本(默認1.1,前端爲squid2.5使用1.0)
gzip_comp_level 2; #壓縮等級
gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型
gzip_vary on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /status { #設定查看Nginx狀態的地址
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx -t 查看編譯的狀態
nginx -s reload 重新啓動服務
3整合
cd /usr/local/tomcat/webapps/ROOT/
vi test.jsp
The time is:<%= location / {
new java.util.Date() %>
vi /usr/local/nginx/conf/nginx.conf
user www www; 1-2行
worker_processes 8;
location / { 48-59行
root html;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
}
location ~\.jsp$ {
proxy_pass http://192.168.0.1:8080;
}
}
}
這樣你輸入ip地址加測試文件,就能用nginx看到動態的測試東東了html
二 nginx+tomcat負載均衡前端
修改配置文件
vi /usr/local/nginx/conf/nginx.conf
http { 17-22
upstream tomcat {
server 192.168.0.1:8080;
server 192.168.0.2:8080;
}
default_type application/octet-stream; 82-86
location ~\.jsp$ {
proxy_pass http://tomcat;
}
}
}
三.nginx+tomcat負載均衡同一ip地址訪問同一機器
1.下載軟件nginx-sticky-module-1.0.tar.gz
2.進入nginx-1.02重新編譯
cd cd nginx-1.0.2
./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-sticky-module-1.0(軟件本身的絕對路徑)
3.中止nginx
nginx -s stop
4.make && mak install
5.vim /usr/local/nginx/conf/nginx.conf
http {
upstream tomcat {
sticky;(19行體添加)
server 192.168.0.1:8080;
server 192.168.0.2:8080;
}java
四.nginx+tomcat+memcached系統環境:rhel6 x64 selinux and iptables disabled
主機角色:node1: 192.168.0.91:nginx tomcat memcached
node2: 192.168.0.92:tomcat memcached
軟件下載:http://www.nginx+tomcat+memcachednginx.org
http://code.google.com/p/memcached-session-manager/
1.下載軟件
Kryo,此實驗咱們採用 kryo 方式。
2.將其移動到 cd /usr/local/tomcat/lib
3. 關閉nginux
4.安裝 memcached
yum install memcached -y
5.啓動memcached
/etc/init.d/memcached start
6.vi /usr/local/tomcat/conf/context.xml
複製
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
...
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:localhost:11211,n2:localhost:11212"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
修改成(最後行)
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.0.1:11211,n2:192.168.0.2:11211"
failoverNodes="n1"
7.啓動nginux
8.vi /usr/local/tomct/webapps/Root/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
node