[ 总结 ] 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 ...
随机推荐
- SpringBoot 中使用shiro注解使之生效
在shiroConfig配置类中增加如下代码: /** * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro ...
- Linux在线用户通信
1,/etc/motd文件 该文件即 message of today(布告栏信息),每次用户登录时,/etc/motd文件的内容会显示在用户的终端.系统管理员可以在文件中编辑系统活动消息 即像公告栏 ...
- qemu的drive参数解释
drive参数很简单,可以理解成是定义了一个实际的硬盘(或者是cd)与drive对应的是device-drive option[,option[,option[,...]]] Define a new ...
- 玩ktap
1.ktap是否有过滤的功能,之前bpf程序可以阻止某些trace的log的输出,ktap是否有这样的功能呢? 2.ftrace 和 perf 的ring buffer好像不是一个,有什么区别? 需求 ...
- jQuery添加、移除、改变class属性
jQuery中一般有3个关于改变元素class的函数addClass.removeClass.toggleClass addClass描述: 为每个匹配的元素添加指定的样式类名$('div').add ...
- IPad Pro 2018 & sketch
IPad Pro 2018 & sketch https://sketch.cloud/s/MyY5w/LJmLgW
- [Java] Java常见错误
1.处理java错误"编码 GBK 的不可映射字符" (1)首先记事本打开java源文件 (2)然后另存为,选择ANSI编码 (3)覆盖 (4)再试一下,ok,编译通过.
- Java 利用枚举实现单例模式
引言 单例模式比较常见的实现方法有懒汉模式,DCL模式公有静态成员等,从Java 1.5版本起,单元素枚举实现单例模式成为最佳的方法. Java枚举 基本用法 枚举的用法比较多,本文主要旨在介绍利用枚 ...
- oracle序列使用时 先用伪列将序列的id调整到正确的位置
- P1419 寻找段落
题目描述 给定一个长度为n的序列a_i,定义a[i]为第i个元素的价值.现在需要找出序列中最有价值的“段落”.段落的定义是长度在[S,T]之间的连续序列.最有价值段落是指平均值最大的段落, 段落的平均 ...