操作系统: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;#    }#}}

转载于:https://www.cnblogs.com/hukey/p/5359507.html

[ 总结 ] nginx 负载均衡 及 缓存相关推荐

  1. Nginx 负载均衡和缓存服务实战

    转自:泥瓦匠BYSocket sf.gg/a/1190000014893012 基础篇 一.环境 二.Nginx是什么? 三.我们为什么选择Nginx? 三.安装与目录 四.基本配置 五.模块 场景实 ...

  2. nginx负载均衡 页面缓存

    nginx的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight  指定轮询几率,weigh ...

  3. Nginx配之负载均衡、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y (若不能安装,执行命令yum install epel-release) 2. 启动.停止 ...

  4. Nginx负载均衡集群介绍

    第1章 集群介绍 1.1 集群简介 1.1.1 什么是集群 简单说,集群就是一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行 ...

  5. 解析nginx负载均衡

    摘要:对于一个大型网站来说,负载均衡是永恒的话题.随着硬件技术的迅猛发展,越来越多的负载均衡硬件设备涌现出来,如F5 BIG-IP.Citrix NetScaler.Radware等等,虽然可以解决问 ...

  6. Nginx负载均衡常用配置

    搭建实验环境(使用docker部署两台nginx容器) 1)使用搭建第一台nginx服务 [root@linux-node4 ~]# docker container run -d --name we ...

  7. 两台linux服务器负载均衡代码实现,nginx实现负载均衡,nginx负载均衡确保两台服务器数据保...

    nginx实现负载均衡,nginx负载均衡确保两台服务器数据保 一.准备篇: Nginx 负载服务器: Centos 6.2 IP:192.168.1.93 WEB服务器: Web1:192.168. ...

  8. nginx负载均衡简单配置

    nginx负载均衡简单配置 准备三台虚拟机来做这个实验: 172.16.160.99        web服务器 172.16.160.103      web服务器 172.16.160.98    ...

  9. Zookeeper实践与应用-- Nginx负载均衡差异

    Nginx/ZooKeeper 负载均衡的差异 Nginx 是我们常见的反向代理服务器,也被广泛的用作负载均衡服务器 ZooKeeper是分布式协调服务框架,有时也被用来做负载均衡 Nginx Ngi ...

最新文章

  1. 【原创】flume-1.3.0安装配置以及flume启动说明
  2. 前端优秀博客网站收集
  3. 详解网络摄像机中的IR-CUT
  4. 使用Ubuntu的公用文件夹轻松地在计算机之间共享文件
  5. 【渝粤教育】 国家开放大学2020年春季 2773特种动物养殖 参考试题
  6. html5 密码框明文,elementUI的密码框的密文和明文
  7. Maven的Settings.xml配置文件解释
  8. gn fast-gn_GN的完整形式是什么?
  9. 《福布斯》:微软的印度未来
  10. javascript-练习-数组数据存li中
  11. VMware VSAN 高级参数介绍(测试用)
  12. Redis学习总结(12)——Redis常见面试题再总结
  13. 提出问题之后,对于回答问题内容的仔细确认!!!(一个字一个字确认!!)
  14. win10安装pycocotools遇到的问题
  15. qq机器人升级最新教程
  16. 车辆vin信息(含发动机号)
  17. #define 喵 int_发现这个领养日,狗子觉得不能让喵独占鳌头
  18. Centos使用chrony做时间同步
  19. 记一次burpsuite安装问题的解决(包括打不开burp-loader-keygen.jar,点击run无反应,回显-Xbootclasspath/p is no longer a sup
  20. discuz建站视频教程 免费个人建站视频教程

热门文章

  1. 兄弟们,TechEd见!
  2. 个人计算机用户隐私保护全接触(2)
  3. Linux04-文件系统权限与ACL权限
  4. 机器学习数据拆分_解释了关键的机器学习概念-数据集拆分和随机森林
  5. 如何使用FaunaDB + GraphQL
  6. ios单应用模式_如何为iOS 13暗模式设置应用
  7. 观察内核linux行为,Linux 学习:基于proc观察Linux行为
  8. restful url 设计规范_restFul接口设计规范
  9. 所有类是object的子类,但是又可以继承一个其他类解析
  10. Spring学习系列(二) 自动化装配Bean