3台linux服务器,其中两台tomcat作应用,一台nginx作两台tomcat的负载
OS:redhat 5.4
app:tomcat6
nginx:1.0.5
2、实现功能
通过配置nginx,实现负载均衡,会话保持,高速缓存静态文件以及手动清除缓存文件
3、所需组件
所有文件放在/home/install/目录下
pcre-8.12.zip 、openssl-1.0.0d.tar.gz、nginx-upstream-jvm-route-0.1.tar.gz、ngx_cache_purge-1.3.tar.gz
4、安装过程
安装会话保持组件
cd nginx - 1.0.5
patch - p0 < /home/install/nginx_upstream_jvm_route/jvm_route.patch
减压各组件
配置nginx
./configure --prefix=/usr/local/nginx --with-http_ssl_module --without-http_fastcgi_module --with-http_flv_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/cgi/ --with-openssl=/home/install/openssl/ --add-module=/home/install/nginx_upstream_jvm_route --with-http_stub_status_module --add-module=/home/install/ngx_cache_purge-1.3 --with-http_perl_module --with-pcre=/home/install/pcre-8.12
部分组件说明
会话保持: --add-module=/home/install/nginx_upstream_jvm_route
缓存清除组件: --add-module=/home/install/ngx_cache_purge-1.3
编译安装
make &&make install
5、配置nginx.conf
#Nginx.conf version Nginx 1.0.5
#by Wugh Email:
[email protected] QQ:5951083
user nobody;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
limit_zone one $binary_remote_addr 10m;
open_file_cache max=51200 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size 4k;
client_max_body_size 8m;
server_name_in_redirect off;
include gzip.conf;
proxy_temp_path /opt/data/proxy_temp_dir;
proxy_cache_path /opt/data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=20g;
upstream www.test.com {
server 192.168.0.10 srun_id=server1 weight=6;
server 192.168.0.20 srun_id=server2 weight=4;
jvm_route $cookie_JSESSIONID reverse;
}
server
{
listen 80;
server_name www.test.com;
#oldweb cache files
location ~ /oldweb(/.*)
{
#allow 192.168.0.0/24;
#deny all;
proxy_cache_purge cache_one $scheme$host$1$is_args$args;
error_page 405 =200 /oldweb$1;
access_log off;
}
#auto purge webold cache
if ( $request_method = "PURGE" ) {
rewrite ^(.*)$ /oldweb$1 last;
}
#auto "/"
# if (-d $request_filename){
# rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
# }
#pass files
location /
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www.test.com;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
access_log off;
}
#cache files 只缓存htm、html、gif、jpg、jpeg、png、ico、css、js、txt、flv、swf、doc、ppt、xls、docx、pptx、xlsx、pdf文件
#并通过"Ctrl+F5"刷新清除指定缓存文件
location ~ .*\.(htm|html|gif|jpg|jpeg|png|ico|css|js|txt|flv|swf|doc|ppt|xls|docx|pptx|xlsx|pdf)$
{
if ($http_Cache_Control = "no-cache") {
rewrite ^(.*)$ /oldweb$1 last;
}
add_header X-Cache $upstream_cache_status;
proxy_cache cache_one;
proxy_cache_valid 200 304 2m;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $scheme$host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://www.test.com;
expires 1d;
access_log off;
}
#nginx status 显示在线情况
location /fwtj {
#allow 192.168.0.0/24;
#deny all;
stub_status on;
access_log off;
}
}
}
6、配置gzip.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
7、操作系统sysctl.conf优化
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename
# Useful for debugging multi-threaded applications
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Controls the maximum size of a message, in bytes
kernel.msgmnb = 65536
# Controls the default maxmimum size of a mesage queue
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 4096 4194304
net.ipv4.tcp_wmem = 4096 4096 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 786432 2097152 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
8、nginx常用命令
检查配置文件是否错误: ./nginx -t
启动nginx:./nginx
重新加载配置文件:/nginx -s reload 或 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
停止nginx: ./nginx -s stop