[ 总结 ] nginx 负载均衡 及 缓存
操作系统: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:访问到的几率要比127.0.0.1:高一倍。
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.
built by gcc 4.4. (Red Hat 4.4.-) (GCC)
built with OpenSSL 1.0.1e-fips Feb
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 ; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; pid logs/nginx.pid; worker_rlimit_nofile ; events {
use epoll;
multi_accept on;
worker_connections ;
} 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= inactive=20s min_uses= 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 ; sendfile on;
tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ;
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= inactive=20s;
open_file_cache_valid 30s;
open_file_cache_errors on;
open_file_cache_min_uses ; gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_comp_level ;
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 ;
proxy_read_timeout ;
proxy_send_timeout ;
proxy_buffer_size 16k;
proxy_buffers 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: weight= max_fails= fail_timeout=;
server 127.0.0.1: weight= max_fails= fail_timeout=;
}
server {
listen ;
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$$is_args$args;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 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;
# }
#} }
[ 总结 ] nginx 负载均衡 及 缓存的更多相关文章
- nginx负载均衡 页面缓存
nginx的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight ...
- 基于【 centos7】四 || FastDFS集群+Nginx负载均衡
1. 架构设计 1.1 架构图 FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用F ...
- Nginx反向代理 负载均衡 页面缓存 URL重写及读写分离
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- Nginx配之负载均衡、缓存、黑名单和灰度发布
一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...
- nginx实现负载均衡、缓存功能实战
nginx实现负载均衡.缓存功能实战 什么是正向代理?应用场景:翻墙 什么是反向代理?例如:haproxy和nginx Nginx实现反向代理 nginx代理基于是ngx_http_proxy_m ...
- 循序渐进nginx(二):反向代理、负载均衡、缓存服务、静态资源访问
目录 反向代理 使用 1.创建代理目标服务端: 2.配置nginx反向代理目标服务端: 3.测试使用: 负载均衡 使用 1.准备服务端 2.修改nginx配置 3.测试 负载均衡策略 负载均衡的额外参 ...
- nginx负载均衡基于ip_hash的session粘帖
nginx负载均衡基于ip_hash的session粘帖 nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除 ...
- 烂泥:nginx负载均衡
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天我们来学习下有关nginx的负载均衡配置.nginx的负载均衡是通过nginx的upstream模块和proxy_pass反向代理来实现的. 说明: ...
- nginx负载均衡 加权轮询和ip_hash
下面给大家总结了几种真正的nginx负载均衡的功能了,在此我们加了一个权重判断法就是根据nginx负载的状态实现分配访问用户到权重值少的机器了,具体配置如下. nginx为后端web服务器(apach ...
随机推荐
- Turtle模块,一个超精简但功能齐全的绘图包
先上官方链接https://docs.python.org/3.3/library/turtle.html 再上一个GitHub上别人做的一个小程序,画小猪佩琦的,里面用到了大量常用的turtle接口 ...
- video on web
一.video容器 你可能经常看到.avi或.mp4的视频文件,实际上avi或者mp4只是一种视频容器.打个比方,ZIP的压缩文件可以包含各种各样的文件,同理,视频容器也定义用来怎么存放各种 ...
- 页面加载时给的子元素的第一个元素加class
HTML代码: <div id="xiao"> <ul> <li></li> </ul> </div> js ...
- 【题解】SHOI2001化工厂装箱员
————传送:洛谷P2530 这道题目还是挺简单的,状态也容易想到. 数据范围非常的小,所以即便是很多维度,复杂度也完全可以接受.定义状态:dp[i][a][b][c]为手上的货物拿到第i个时三种物品 ...
- 【翻译】为什么Java中的String不可变
笔主前言: 众所周知,String是Java的JDK中最重要的基础类之一,在笔主心中的地位已经等同于int.boolean等基础数据类型,是超越了一般Object引用类型的高端大气上档次的存在. 但是 ...
- Android-使用ViewFlipper实现轮番切换广告栏
所谓的轮番切换广告栏,指的是下面这个东西,笔主不知道该怎么确切描述这货... 笔主没有百度研究过其他大牛是怎么实现这个功能的,在这里笔主充分发挥DIY精神,利用ViewFlipper闭门土制了一个,下 ...
- yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY 博客分类: linux 三种解决方案 我采取第三种方案解决的 第一种: linu ...
- Unescape JavaScript's escape() using C#
js里面的 unescape escape 对应C#里面 var unescapedString = Microsoft.JScript.GlobalObject.unescape(yourEscap ...
- codeforces 1015C
C. Songs Compression time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Bash 实例,第一部分
您可能要问:为什么要学习 Bash 编程?好,以下是几条令人信服的理由: 已经在运行它 如果查看一下,可能会发现:您现在正在运行 bash.因为 bash 是标准 Linux shell,并用于各种目 ...