操作系统:centos6.4 x64
前端使用nginx做反向代理,后端服务器为:apache + php + mysql
1. nginx负载均衡。
nginx编译安装(编译安装前面的文章已经写过)、apache + php + mysql 直接使用yum安装。
nginx端口:80
apache端口:800 和 8080
nginx配置如下:
在http节点下添加如下:
upstream backend {
ip_hash;server 127.0.0.1:8080 weight=5 max_fails=2 fail_timeout=30s;server 127.0.0.1:800 weight=10 max_fails=2 fail_timeout=30s;}
将server节点下的location节点中添加proxy_pass 配置为:http://+upstream名称
location / {proxy_pass http://backend; #root html;#index index.html index.htm;}
这样负载均衡就已经完成了。upstream策略如下:
weight 权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。上面的情况:127.0.0.1:800访问到的几率要比127.0.0.1:8080高一倍。
ip_hash:
每个请求按访问IP的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream还可以为每个设备设置状态值,这些状态如下:
down 表示当前的server暂时不参与负载。
weight 默认为1,weight越大,负载的权重就越大。
max_fails 允许请求失败的次数默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误。
fail_timeout max_fails次失败后,暂停的时间。
backup 其他所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力是最小的。
2. nginx缓存设置
建议:在设置nginx缓存时,最好能编译安装ngx_purge_module 模块。
[root@cloud conf]# nginx -V nginx version: nginx/1.9.9 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=../ngx_cache_purge-2.3 \
--with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module
nginx 全部相关配置如下:
user nginx nginx; worker_processes 2;error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;pid logs/nginx.pid;worker_rlimit_nofile 65535;events {use epoll;multi_accept on;worker_connections 4096; }http {server_tokens off;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;open_log_file_cache max=2000 inactive=20s min_uses=2 valid=1m;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;limit_conn_zone $binary_remote_addr zone=addr:5m;limit_conn addr 100;sendfile on;tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;client_header_timeout 2m;client_body_timeout 3m;reset_timedout_connection on;send_timeout 15s;client_max_body_size 10m;client_body_buffer_size 512k;open_file_cache max=65535 inactive=20s;open_file_cache_valid 30s;open_file_cache_errors on;open_file_cache_min_uses 2;gzip on;gzip_disable "msie6";gzip_proxied any;gzip_comp_level 4;gzip_vary on;gzip_min_length 1k;gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javasc ript;proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 4 64k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区proxy_temp_path /cache/proxy_temp_dir; # 创建临时缓存目录
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。proxy_cache_path /cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend {ip_hash;server 127.0.0.1:8080 weight=5 max_fails=2 fail_timeout=30;server 127.0.0.1:800 weight=10 max_fails=2 fail_timeout=30;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://backend;proxy_cache cache_one;proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;expires 1d;#root html;#index index.html index.htm;}location ~ /purge(/.*) {proxy_cache_purge cache_one $host$1$is_args$args;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1; #}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one ##location ~ /\.ht {# deny all;#}}# 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;# }#}}